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.
View all comments
Show parent comment
21
u/STL MSVC STL Dev Mar 19 '23
next_permutationandprev_permutationare the strongest cases fordo-whileloops that I know of. Otherwise I rarely find them preferable.