7
$\begingroup$

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?

$\endgroup$

2 Answers 2

3
$\begingroup$

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)
$\endgroup$
8
$\begingroup$

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
        }
    }
}
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .