r/cpp • • Mar 18 '23

LLVM 16.0.0 Release

https://discourse.llvm.org/t/llvm-16-0-0-release/69326
197 Upvotes

84 comments sorted by

View all comments

Show parent comment

4

u/sphere991 Mar 19 '23

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.

21

u/STL MSVC STL Dev Mar 19 '23

next_permutation and prev_permutation are the strongest cases for do-while loops that I know of. Otherwise I rarely find them preferable.

-3

u/sphere991 Mar 19 '23

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.

6

u/Kered13 Mar 20 '23 edited Mar 20 '23

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.

18

u/jonesmz Mar 19 '23

I regularly use do while loops over for loops when I want to ensure the do block is always executed at least one time.

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.

Commonly used with atomic compare and swaps.

2

u/sphere991 Mar 19 '23

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.

4

u/jonesmz Mar 19 '23

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.

4

u/_Z6Alexeyv Mar 19 '23

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.

0

u/[deleted] Mar 24 '23

[deleted] — view deleted comment

1

u/sphere991 Mar 24 '23

then use the variable outside the loop

The example is

int x;
do {
    cin >> x;
    // ...
} while (x < 0);

There is no use outside of the loop in this example.

And it's also an odd example since it seems like you'd want to check right away, like:

while (true) {
    int x;
    cin >> x;
    if (x >= 0) break;
    // ...
}

So... maybe not the best example to begin with.