RFC: Behavior of continue in do/while loops.
renoX
renosky at free.fr
Wed Sep 12 12:42:40 PDT 2007
Downs a écrit :
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> 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_.
Yes, that's what I would expect.
> 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.
Bah, a matter of opinion, I interpret 'continue' as 'continue to the
end', why would 'continue to the beginning' more intuitive?
> 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_.
But even in a for loop, continue doesn't really continue to the
beginning of the loop, it continue to the 'termination test'.
> 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)
No, current continue behaviour is consistent for while/for/do .. while
which is good.
Maybe a different keyword could be more intuitive, but I can't find any
(skip has the same issue, goto_end is not clear if it's break or
continue, etc) unless you want to start using keyword like
continue_to_end_of_iteration..
> 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)
No:
1) the continue behaviour is coherent.
2) it's consistent with C's behaviour.
There are many thing that I dislike in C (stupid variable declaration
syntax, use of && for shortcut-and instead of & even though shortcut-and
are much more used than binary-and, use 0 to start octal instead of 0o
,etc) but continue isn't one of these (except that it should take a
'loop label' as an optional argument as D's continue does).
renoX
>
> Looking forward to your comments,
> --downs
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.7 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFG6AvvpEPJRr05fBERAnfQAJ0UapoxtHQCtIfTerkFe3iUnTHXtACfTFj4
> ennSYblhmnx1yOzb6izeCfs=
> =kBA4
> -----END PGP SIGNATURE-----
More information about the Digitalmars-d
mailing list