Break/Continue Structure

Derek Parnell derek at psych.ward
Thu Mar 23 16:14:11 PST 2006


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 ...

 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[].

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
24/03/2006 11:07:48 AM



More information about the Digitalmars-d-learn mailing list