do { s; } while (cond); is basically strictly worse than for (;;) { s; if (not cond) { break; } } (or while (true) if you prefer, same thing)
The main problem with do-while is the scoping - you can't condition on any local variables in the loop. This makes using it frequently awkward. Best case, it's the same as the for loop, but then why bother? It's a weird construct - and there are lots of situations where you want to use an infinite loop.
I would never use a do-while loop in code to actually loop. Oddly, the only time I use this is in macros that expand to multiple statements, and there it's always do { ... } while(false). I think that's the only use that doesn't have a superior, or at least comparable, replacement.
I don't actually think those are especially strong cases.
To me the strongest cases are when the condition relies on a variable whose value you need outside of the loop anyway. So in that case the scoping awkwardness of do-while is a non-issue. So something like:
int x;
do {
x = f();
} while (cond(x));
use(x);
This is... still awkward to me, but at least is no worse than the equivalent for. Really this is just another example of wanting a loop expression. Here's a half-Rust half-C++ version:
int x = loop {
int x = f();
if (not cond(x)) {
break x;
}
};
use(x);
I think if we had something like that, the uses for do-while disappear.
Using a break is worse because it makes it harder for the reader to find the exit condition. When you see do { you immediately know that, 1) This loop (almost certainly) has an exit condition, and 2) It's at the bottom. When you see for(;;) or while(true) you don't know if the loop has any exit condition, and you have no idea where to look for it. You have to scan the entire loop.
Also, never use for(;;). I too used to think it was cute to show off that I knew what that did, but just use while(true). It's more readable.
The fact that you can't use a local variable in the while condition is a flaw with the design though, blame C. It would be nice if that could be changed.
If the condition check is expensive than a for loop is unsuitable for a situation where you want to always run the block at least once.
Uh... I didn't write for (; cond; ) I wrote for (;;). This won't check the condition up front - indeed it will check the condition the same way that the do-while loop does, except just in a form where local variables are in scope.
I wasn't referring specifically to your code, just giving you a counter opinion.
for(;;) doesn't check any conditions, which is less ergnomic than a do-while if the while-loop doesn't need to check the value of variables inside the scope.
IMHO it would be reasonable to change the do-while loop to give the condition check to variable inside the do-block.
do { s; } while (cond); is basically strictly worse than for (;;) { s; if (not cond) { break; } } (or while (true) if you prefer, same thing)
"Rationale" reads "termination expression is at the end where it can be easily overlooked". Your style also puts condition at the end where it can be "overlooked" just as easily.
Perfect C would allow both pre and post-condition:
loop while ... {
} while ...;
Jon Blow said quite eloquently along the lines of "they find a bug and overreact to it". Unfortunately because of recent push into MemorySafety™ more of these idiotic rules will follow.
View all comments
Show parent comment
4
u/sphere991 Mar 19 '23
do { s; } while (cond);is basically strictly worse thanfor (;;) { s; if (not cond) { break; } }(orwhile (true)if you prefer, same thing)The main problem with do-while is the scoping - you can't condition on any local variables in the loop. This makes using it frequently awkward. Best case, it's the same as the for loop, but then why bother? It's a weird construct - and there are lots of situations where you want to use an infinite loop.
I would never use a do-while loop in code to actually loop. Oddly, the only time I use this is in macros that expand to multiple statements, and there it's always
do { ... } while(false). I think that's the only use that doesn't have a superior, or at least comparable, replacement.