<p>In languages such as C, to <code>break</code>/<code>continue</code> out of an inner loop, there is no direct way so one must use <code>goto</code>:</p>
<pre><code>while (condition) {
    continue_outer:
    while (condition) {
        // goto break_outer;
        // goto continue_outer;
    }
}
break_outer:
</code></pre>
<p>What are some more direct was to support breaking or continuing from an outer loop?</p>

<control-flow><loops>
What are the different options for breaking out of an inner loop?
<h2>Early returns</h2>
<p>Python notably doesn't allow labelled <code>break</code> and <code>continue</code> statements. Instead, when it's necessary to break from a nested loop, the programmer can write the nested loop in a function and use <code>return</code> to terminate the loop early.</p>
<h2>Cartesian products</h2>
<p>Instead of having a way to <code>break</code> from nested loops, you can give the programmer a way to write their code with just one loop. Python's standard library contains the helpful <a href="https://docs.python.org/3/library/itertools.html#itertools.product" rel="nofollow noreferrer"><code>itertools.product</code></a> which can be used to refactor nested <code>for</code> loops into a single level. For example, these two loops iterate over the same values in the same order:</p>
<pre class="lang-py prettyprint-override"><code>for x in xs:
    for y in ys:
        for z in zs:
            ...
</code></pre>
<pre class="lang-py prettyprint-override"><code>from itertools import product

for x, y, z in product(xs, ys, zs):
    ...
</code></pre>
<h2>Short-circuiting functions for iteration</h2>
<p>If you want to terminate a loop early, it's likely because you're doing some kind of search and you want to stop when you find the thing being searched for. So your language's standard library can provide helper functions for doing such searches, and the helper functions can handle the termination condition so that the programmer doesn't need to <code>break</code> explicitly.</p>
<p>Using Python again as an example, <code>any</code> and <code>all</code> are two such functions in the standard library, which can accept &quot;nested loops&quot; in the form of generator expressions. These are equivalent:</p>
<pre class="lang-py prettyprint-override"><code>any_found = False
for x in xs:
    for y in ys:
        if some_condition(x, y):
            any_found = True
            break
</code></pre>
<pre class="lang-py prettyprint-override"><code>any_found = any(some_condition(x, y) for x in xs for y in ys)
</code></pre>

CC BY-SA 4.0
2023-05-19T02:36:24.543Z
2023-05-19T02:36:24.543Z
kaya3
<h1>Depth Number</h1>
<p>One option is for <code>break</code> and <code>continue</code> to accept a number, being the number of extra loops to break out of. A <code>break;</code> would be equivalent to <code>break 0;</code>.</p>
<pre><code>while (condition) {
    while (condition) {
        // break 1;
        // continue 1;
    }
}
</code></pre>
<h1>Tagged Loops</h1>
<p>Another way that does not involve <code>goto</code> is to label the loops themselves and break a loop corresponding to a certain label. Java and Swift do this.</p>
<pre><code>outer: while (condition) {
    while (condition) {
        // break outer;
        // continue outer;
    }
}
</code></pre>
<h1>Breaking Construct Type</h1>
<p>A third way, while less capable than the others, could still be useful, is to use the keyword for the construct in the <code>break</code> or <code>continue</code> statement.</p>
<pre><code>while (condition) {
    for (init; condition; increment) {
        switch (value) {
            case 10:
                print(&quot;Value is 10&quot;);
                break switch; // Breaks the switch
            case 100:
                break while; // Exits the entire while loop
            default:
                break for; // Breaks the for loop
        }
    }
}
</code></pre>

CC BY-SA 4.0
2023-05-19T00:32:53.470Z
2023-05-20T17:13:34.963Z
2023-05-20T17:13:34.963Z
CPlus
# What are the different options for breaking out of an inner loop?
Posted by: username_0 on 2023-05-19
In languages such as C, to break/continue out of an inner loop, there is no direct way so one must use goto:
while (condition) {
    continue_outer:
    while (condition) {
        // goto break_outer;
        // goto continue_outer;
    }
}
break_outer:

What are some more direct was to support breaking or continuing from an outer loop?
**Tags:** control-flow loops
---
## 2 answers
### Answer by username_1 on 2023-05-19. Score: 3 | Accepted answer
Early returns
Python notably doesn't allow labelled break and continue statements. Instead, when it's necessary to break from a nested loop, the programmer can write the nested loop in a function and use return to terminate the loop early.
Cartesian products
Instead of having a way to break from nested loops, you can give the programmer a way to write their code with just one loop. Python's standard library contains the helpful itertools.product which can be used to refactor nested for loops into a single level. For example, these two loops iterate over the same values in the same order:
for x in xs:
    for y in ys:
        for z in zs:
            ...

from itertools import product

for x, y, z in product(xs, ys, zs):
    ...

Short-circuiting functions for iteration
If you want to terminate a loop early, it's likely because you're doing some kind of search and you want to stop when you find the thing being searched for. So your language's standard library can provide helper functions for doing such searches, and the helper functions can handle the termination condition so that the programmer doesn't need to break explicitly.
Using Python again as an example, any and all are two such functions in the standard library, which can accept "nested loops" in the form of generator expressions. These are equivalent:
any_found = False
for x in xs:
    for y in ys:
        if some_condition(x, y):
            any_found = True
            break

any_found = any(some_condition(x, y) for x in xs for y in ys)
---
### Answer by username_0 on 2023-05-19. Score: 8
Depth Number
One option is for break and continue to accept a number, being the number of extra loops to break out of. A break; would be equivalent to break 0;.
while (condition) {
    while (condition) {
        // break 1;
        // continue 1;
    }
}

Tagged Loops
Another way that does not involve goto is to label the loops themselves and break a loop corresponding to a certain label. Java and Swift do this.
outer: while (condition) {
    while (condition) {
        // break outer;
        // continue outer;
    }
}

Breaking Construct Type
A third way, while less capable than the others, could still be useful, is to use the keyword for the construct in the break or continue statement.
while (condition) {
    for (init; condition; increment) {
        switch (value) {
            case 10:
                print("Value is 10");
                break switch; // Breaks the switch
            case 100:
                break while; // Exits the entire while loop
            default:
                break for; // Breaks the for loop
        }
    }
}
---
