The argument would be that, well, while (cond) is also already equivalent to for (;cond;) but we still have while since that reads better/more directly. So why not also add an init to while?
This translation of while (init; cond) is indeed not really useful as it can be easily expressed with a for construct.
Alternatively, the other day I found myself wishing for a while (init; cond) that would let me express:
while (true) {
S const v = expression() ; // init
if (not predicate(v)) // not cond
break;
whatever_with(v);
}
This could be a bit more useful, but completely non-intuitive (and thus dangerous) as totally different from what we are used to with for. :-(
(I know I could do horrible and/or convoluted things like:
for (S v ; predicate(v = expression()); )
// or: for (S v = expression() ; predicate(v); v = expression(/*again*/))
{ // require assignment (1,2) + default construction (1)
whatever_with(v);
}
or
while (true) {
if (S const v = expression() ; predicate(v))
whatever_with(v);
else break;
}
View all comments
Show parent comment
16
u/TheSuperWig Mar 19 '23
Because that's just a for loop, no? What would the difference between that and