for x in range(10):
    for y in range(10):
        print(x * y)
        if x * y > 50:
            break
    else:
        continue  # only executed if the inner loop did NOT break
    break  # only executed if the inner loop DID break

The same works for deeper loops:

for x in range(10):
    for y in range(10):
        for z in range(10):
            print(x, y, z)
            if (x * y * z) == 30:
                break
        else:
            continue
        break
    else:
        continue
    break
Answer from Markus Jarderot on Stack Overflow
Discussions

Breaking/continuing out of multiple loops - Ideas - Discussions on Python.org
Hi! SUGGESTION An easy way to break/continue within nested loops. MY REAL LIFE EXAMPLE I am looping through a list of basketball players, each of which has multiple tables, each of which can have up to three different versions. I manipulate data in the deepest loop and, if some key data is ... More on discuss.python.org
🌐 discuss.python.org
4
August 21, 2022
Break from multiple loops at once - Ideas - Discussions on Python.org
Often, I want to use break to break not only out of the innermost loop, but also out of further loops. It’s a lot more inconvenient to have to set a looping boolean before the outer loop, set it to False in the if clause, and check for it in the outer loop and break if it’s False. More on discuss.python.org
🌐 discuss.python.org
1
September 15, 2025
How to break out of nested loops in python? - Stack Overflow
Copyis_looping = True for i in ... loop if not is_looping: break # break out of outer loop ... Sign up to request clarification or add additional context in comments. ... Save this answer. ... Show activity on this post. In Python you can write an else clause for a loop, ... More on stackoverflow.com
🌐 stackoverflow.com
control flow - What are the different options for breaking out of an inner loop? - Programming Language Design and Implementation Stack Exchange
What are some more direct was to support breaking or continuing from an outer loop? ... Python notably doesn't allow labelled break and continue statements. More on langdev.stackexchange.com
🌐 langdev.stackexchange.com
May 19, 2023
🌐
Note.nkmk.me
note.nkmk.me › home › python
Break Out of Nested Loops in Python | note.nkmk.me
August 18, 2023 - Python for loop (with range, enumerate, zip, and more) You can break all loops with else and continue. for i in l1: for j in l2: print(i, j) if i == 2 and j == 20: print('BREAK') break else: continue break # 1 10 # 1 20 # 1 30 # 2 10 # 2 20 # BREAK ...
🌐
Python Morsels
pythonmorsels.com › breaking-out-of-a-loop
Breaking out of a loop - Python Morsels
June 26, 2025 - Python's break statement stops the loop entirely and continue stops a single iteration of a loop.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-break-out-of-multiple-loops-in-python
How to Break out of multiple loops in Python ? - GeeksforGeeks
February 24, 2023 - If the flag variable is True, then the if block will be executed and will break out of the inner loop also. Else, the outer loop will continue. ... # Python code to break out of multiple # loops by using a flag variable def elementInArray(arr, ...
🌐
Coursera
coursera.org › tutorials › python-break
How to Use Python Break | Coursera
You can use break to exit for loops and while loops. Break only exits the innermost loop in a nested loop. You can’t use break to exit an if statement unless the if statement is inside of a loop.
Find elsewhere
🌐
Quora
quora.com › How-do-I-break-out-of-a-specific-inner-or-outer-loop-in-python
How to break out of a specific inner or outer loop in python - Quora
Answer (1 of 7): You can break out of an inner loop using break statements. In a nested loop the [code ]break[/code] statement only terminates the loop in which it appears. For example: [code]for i in range(1, 5): print("Outer loop i = ", i, end="\n\n") for j in range (65, 75): ...
🌐
Python.org
discuss.python.org › ideas
Breaking/continuing out of multiple loops - Ideas - Discussions on Python.org
August 21, 2022 - Hi! SUGGESTION An easy way to break/continue within nested loops. MY REAL LIFE EXAMPLE I am looping through a list of basketball players, each of which has multiple tables, each of which can have up to three different versions. I manipulate data in the deepest loop and, if some key data is ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How To Use break, continue, and pass Statements in Python | DigitalOcean
April 24, 2026 - Python does not support a built-in syntax like break 2 to exit multiple nested loops at once. Instead, you can break out of nested loops using one of the following methods: ... found = False for i in range(3): for j in range(3): if some_condition(i, j): found = True break # Break inner loop if found: break # Break outer loop
🌐
Learn Python
learnpython.dev › 02-introduction-to-python › 110-control-statements-looping › 40-break-continue
break, continue, and return :: Learn Python by Nina Zakharenko
break and continue allow you to control the flow of your loops. They’re a concept that beginners to Python tend to misunderstand, so pay careful attention. Using break The break statement will completely break out of the current loop, meaning it won’t run any more of the statements contained ...
🌐
Medium
medium.com › techtofreedom › 5-ways-to-break-out-of-nested-loops-in-python-4c505d34ace7
5 Ways To Break Out of Nested Loops in Python | by Yang Zhou | TechToFreedom | Medium
January 9, 2022 - As an old saying goes, “comparison is the thief of joy”. Python can’t do this, but others can, such as the PHP: foreach ($a_list as $a) { foreach ($b_list as $b) { if (condition($a, $b)) { break 2; //break out of 2 loops } } }
🌐
AskPython
askpython.com › home › python break statement
Python break Statement - AskPython
July 7, 2022 - Python break statement is used to get out of the loops. We can use it with for loop and while loops. Python break example with nested loops, break outer loop.
🌐
freeCodeCamp
freecodecamp.org › news › break-in-python-nested-for-loop-break-if-condition-met-example
Break in Python – Nested For Loop Break if Condition Met Example
May 17, 2022 - In this article, we'll first see how to use the break statement in for and while loops. Then we'll look at some of the methods we can use to break nested loops in Python.
🌐
Python.org
discuss.python.org › ideas
Break from multiple loops at once - Ideas - Discussions on Python.org
September 15, 2025 - Often, I want to use break to break not only out of the innermost loop, but also out of further loops. It’s a lot more inconvenient to have to set a looping boolean before the outer loop, set it to False in the if clause…
🌐
Delft Stack
delftstack.com › home › howto › python › break out of multiple loops in python
How to Break Out of Multiple Loops in Python | Delft Stack
March 11, 2025 - This article explores two effective methods: using the return statement and the for/else loop. Gain insights into practical examples that can enhance your coding skills and streamline your Python programming experience.
🌐
Quora
quora.com › Can-I-get-away-with-using-one-break-statement-to-get-out-of-all-nested-for-loops-in-Python
Can I get away with using one break statement to get out of all nested for loops in Python? - Quora
I've used Python 2 and 3, and Iron Python in Visual Studio · Author has 5.3K answers and 6.2M answer views · 5y ... The break statement, like in C, breaks out of the innermost enclosing for or while loop. So the answer to your question is no. If you want to leave all the loops when you get a break in an inner loop, you probably need to set a flag just before the break, which is tested in the outer ...
🌐
Quora
quora.com › How-can-you-break-out-of-two-nested-loops-at-once-in-Python
How to break out of two nested loops at once in Python - Quora
Answer: What I’d do in this case, I would pack the nested loops in a function and return. You could also use a flag. First case: [code]def foo(): # some logic for i in i_range: for j in j_range: # some logic if conditions_met: return i, j print(foo()) [/code]Second case: [co...
Top answer
1 of 2
3

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)
2 of 2
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
        }
    }
}