4 ms·
For your first case, why not: do { } while(!do_something()); Not trying to be snarky, I'd just like to understand :)
by alexhutcheson 11y ago
For your first case, why not:
do {
} while(!do_something());
Not trying to be snarky, I'd just like to understand :)
- mikeash 11y agoBecause it's often either like this: do { const bool flag = do_something(); do_something_else(); } while(!flag); Or like this: do { int whatever = get_some_data(); const bool flag = do_something(whatever); } while(!flag);
- p1esk 11y agoSomeone mentioned this instead: while (true) { const bool flag = do_something(); if (!flag) break; } Is it not what grandparent wanted?
- mikeash 11y agoYes, that would do it. It would just be nice to have it be better supported in the language rather than working around it like this. Any time you introduce break or continue into a loop you're making it more difficult to follow what's going on, since suddenly the exit condition can be anywhere. Not a big deal, especially if there's only one and it's right at the end, but mildly annoying that it can't be expressed more naturally.