Behavior of continue in do/while loops.
Steven Schveighoffer
schveiguy at yahoo.com
Wed Sep 12 09:30:31 PDT 2007
"Downs" wrote
> I just came upon the interesting fact that using the "continue"
> statement in a do { } while() loop does not, in fact, continue with the
> next loop, but instead continues with the _condition_.
> I know this is the same behavior as in C, but as somebody who never
> encountered it before I can assure you, it's highly unintuitive.
> The confusion here stems largely from the way a do/while loop looks -
> the condition is found at the _end_ at the loop body, yet I expected
> continue to jump to the _beginning_.
> There is two questions I want to ask:
> First, if you were starting a new language, should the behavior of
> continue in that language match what I expected in this case?
> (theoretical case)
My view of the continue statement is that it means "I am done with this
iteration of the loop, skip to the next one." In that sense, I think the
continue statement works correctly as it is implemented today. BTW, you
could implement what you want by using an infinite loop and a break
statement. i.e.:
do
{
...
} while(condition)
becomes:
while(true)
{
...
if(condition) break;
}
> And second, should the behavior be changed in the D programming
> language, even though it _might_ conceivably break code that relies on
> the current behavior? (practical case)
Nope :) Alienating C/Java developers is not a good idea.
-Steve
More information about the Digitalmars-d
mailing list