Break/Continue Structure

Rory Starkweather starkweatherr at mchsi.com
Fri Mar 24 01:58:40 PST 2006


Derek Parnell wrote:
> On Thu, 23 Mar 2006 17:51:09 -0600, Rory Starkweather wrote:
> 
> 
>>  I guess I would like to ask why I shouldn't do this at the same time I 
>>ask how to do it.
>>
>>  I've been looking at a piece of code like:
>>
>>	foreach (int i, dchar c; theString)
>>	{
>>                 if (c == searchChar)
>>		return i + 1;
>>	}
>>	return 0;
>>}
>>
>>  I understand the reason for doing this, but prefer to do things like this:
>>
>>	int iPointer;
>>	
>>	iPointer = 0;
>>	foreach (int i, dchar c; theString)
>>	{
>>                 if (c == searchChar)
>>		iPointer =  i + 1;
>>		// ?? break;
>>	}
>>	return (iPointer);
>>}
>>
>>  I realize that the extra integer takes up a little memory space, but . . .
>>
>>  My questions are:
>>Will 'break' work here?
> 
> Yes it will, though it should be coded ...

  Understood. The // ?? was added for emphasis.

> 
>  iPointer = 0;
>  foreach (int i, dchar c; theString)
>  {
>    if (c == searchChar)
>    {
>      iPointer =  i + 1;
>      break;
>    }
>  }
>  return (iPointer);
> 
> 
> 
>>Why not do it this way?
> 
> 
> It is just a coding-style issue. People code to different standards.
> 
> BTW, using the foreach this way can be misleading. The pointer value
> returned represents the number of dchars examined and *not* an index into
> theString. This is significant if theString is not a dchar[].

  Good point. Thanks for mentioning it.I hadn't really considered that. 
Another option that has been suggested is using 'ifind' after suitable 
conversions. 'ifind' is pretty much guaranteed to give me a pointer to 
the actual character I want, isn't it?



More information about the Digitalmars-d-learn mailing list