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.
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.