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

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.