Technically every control flow statement is equivalent, i. e. you can rewrite any code that uses one control flow statement in such a way that it uses another control flow statement instead, while arriving at the same result. Not that it would always be a good idea ;)
In your example, if the outer loop is a loop, you can use
continue
instead of break. However, as this would just lead to jump forward to the next iteration of the loop, you also need to remember the fact that you wish to exit the loop and check this in your loop condition:
bool end_of_loop = false;
...
do {
...
...
if (...) {
end_of_loop = true;
continue;
}
...
...
} while (!end_of_loop);
This code will work for any kind of loop:
do
,
for
or
while
, but not other types of code blocks.
Also it really is no improvement over
break
- neither from the point of readability nor performance, so if your goal is to avoid
goto
-like commands, you can instead use
if
inside your code as a means to skip over the rest of the code block:
bool end_of_loop = false;
...
do {
...
...
if (...) {
end_of_loop = true;
}
if (!end_of_loop) {
...
...
}
} while (!end_of_loop);
This will work in any kind of code block, not just loops.