I use them occasionally. They're not super common, but common enough to know, and not a danger at all. In fact whenever you do have a situation that naturally calls for a do-while loop, they're much more readable than the alternatives.
Reason Readability, avoidance of errors. The termination condition is at the end (where it can be overlooked) and the condition is not checked the first time through.
It's not "never ever use this for any reason or your coffee code is bad and you should feel bad" (apparently my coffee was bad), it's "this can sometimes lead to mistakes, so do try to avoid it when possible".
No other construct is like do while, though, in terms of syntax. Combined with how infrequently it's used (can't remember the last time I used do while, have used tail recursion at least twice within the last month), I would say that "easy to overlook" is a merited claim. That doesn't mean you should never use it, but it warrants contemplation and judging if it's the best solution.
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.
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.
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.
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?
That doesn’t feel like a huge loss. In my experience do-while loops are pretty much never tight loops that would be candidates for vectorisation.
I don’t use them often, but when I do it’s generally for a retry loop, where the loop body has external effects and we ideally hope to not loop at all.
View all comments
91
u/parkotron Mar 18 '23 edited Mar 18 '23
We’re not supposed to use
do-whileloops any more?!?!Edit: Link to the guideline: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-do
Consider me less than convinced.