Behavior of continue in do/while loops.

Downs default_357-line at yahoo.de
Wed Sep 12 09:54:34 PDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steven Schveighoffer wrote:
> "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;
> }
Yeah, that's what I ended up doing.

> 
>> 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 
> 
> 
miller[] came up with two good alternatives over in #D
1) forbidding continue in do/while and replacing it with continue
do/continue while, or
2) adding goto do/goto while statements.
The first one has the advantage of making it totally impossible for a
newcomer to trip on this, while the second one has the advantage of not
breaking existing code.

Whaddya think?
 --downs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG6BnJpEPJRr05fBERAsnFAJ4xcsAi5bkk5JRVa+lM/mv+kafurgCcCU0n
/g7nchrjUCIBAW9+EpRgo/g=
=TslT
-----END PGP SIGNATURE-----



More information about the Digitalmars-d mailing list