for ii in range(200):
    for jj in range(200, 400):
        ...block0...
        if something:
            break
    else:
        ...block1...

Break will break the inner loop, and block1 won't be executed (it will run only if the inner loop is exited normally).

Answer from culebrón on Stack Overflow
Top answer
1 of 9
246
for ii in range(200):
    for jj in range(200, 400):
        ...block0...
        if something:
            break
    else:
        ...block1...

Break will break the inner loop, and block1 won't be executed (it will run only if the inner loop is exited normally).

2 of 9
131
for i in ...:
    for j in ...:
        for k in ...:
            if something:
                # continue loop i

In a general case, when you have multiple levels of looping and break does not work for you (because you want to continue one of the upper loops, not the one right above the current one), you can do one of the following

Refactor the loops you want to escape from into a function

def inner():
    for j in ...:
        for k in ...:
            if something:
                return


for i in ...:
    inner()

The disadvantage is that you may need to pass to that new function some variables, which were previously in scope. You can either just pass them as parameters, make them instance variables on an object (create a new object just for this function, if it makes sense), or global variables, singletons, whatever (ehm, ehm).

Or you can define inner as a nested function and let it just capture what it needs (may be slower?)

for i in ...:
    def inner():
        for j in ...:
            for k in ...:
                if something:
                    return
    inner()

Use exceptions

Philosophically, this is what exceptions are for, breaking the program flow through the structured programming building blocks (if, for, while) when necessary.

The advantage is that you don't have to break the single piece of code into multiple parts. This is good if it is some kind of computation that you are designing while writing it in Python. Introducing abstractions at this early point may slow you down.

Bad thing with this approach is that interpreter/compiler authors usually assume that exceptions are exceptional and optimize for them accordingly.

class ContinueI(Exception):
    pass


continue_i = ContinueI()

for i in ...:
    try:
        for j in ...:
            for k in ...:
                if something:
                    raise continue_i
    except ContinueI:
        continue

Create a special exception class for this, so that you don't risk accidentally silencing some other exception.

Something else entirely

I am sure there are still other solutions.

🌐
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 ...
Discussions

Breaking out of nested loops
Ahh yes, one of the rare, legitimate, use cases for a goto statement. Edit: this is downvoted, but this is one of the only places you’ll see them used in the Linux kernel. More on reddit.com
🌐 r/learnpython
8
4
July 28, 2020
tideman, how to break out of a nested inner loop but continue the outer loop
You're asking the wrong question here, because what you're asking for is exactly what's currently happening - Your inner loop exits, but your outer loop doesn't and these lines: locked[pairs[i].winner][pairs[i].loser] = true; num_locked++; get run. If you only want the above lines to be run if this condition: if (is_cycle(pairs[i], pairs[j], num_locked)) is true then the easiest thing to do would be... to move those two lines into that condition, ahead of your break More on reddit.com
🌐 r/cs50
2
4
November 29, 2022
python - How to continue the outer loop? - Stack Overflow
Is there any opportunity or workaround in Python so you can break or continue the outer loop? For example: for i in range(1, 5): for j in listD: if smth: continue More on stackoverflow.com
🌐 stackoverflow.com
How to continue in nested loops in Python - Stack Overflow
I know you can avoid this in the majority of cases but can it be done in Python? ... Use break to leave the inner loop - this'll immediately continue in the outer loop. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Learn Python
learnpython.dev › 02-introduction-to-python › 110-control-statements-looping › 40-break-continue
break, continue, and return :: Learn Python by Nina Zakharenko
Instead, it goes back to the start of the loop, skipping over any other statements contained within the loop. >>> for name in names: ... if name != "Nina": ... continue ... print(f"Hello, {name}") ... Hello, Nina ... # Python file names.py names = ["Jimmy", "Rose", "Max", "Nina", "Phillip"] for name in names: if len(name) != 4: continue print(f"Hello, {name}") if name == "Nina": break print("Done!")
🌐
Verve AI
vervecopilot.com › interview-questions › are-you-misunderstanding-continue-outer-loop-python-and-its-impact-on-interview-success
Are You Misunderstanding Continue Outer Loop Python And Its Impact On… · Its Impact On Interview Success · Interview Q&A | Verve AI
August 28, 2025 - In this output, notice how the inner loop's `j=1` is skipped, but the outer loop continues its execution for `i=0`, `i=1`, `i=2`. The `continue` here does not cause the outer loop to skip to its next iteration. ... A common point of confusion for those learning Python, especially when coming from languages like Java or C++ which might offer labeled `break`/`continue` statements, is that Python does not natively support a direct `continue outer loop python` construct [^2]. There isn't a built-in keyword or syntax like `continue 2` or `continue outer` that allows you to specify which loop level `continue` should apply to.
🌐
LWN.net
lwn.net › Articles › 906512
Python multi-level break and continue [LWN.net]
August 31, 2022 - Suter agreed with that to a certain extent, noting the first search result for "python multiple for loop break" is a Stack Overflow answer that is overly clever. Suter adapted it to the original example as follows: for sport in all_sports: # "for sport" loop for player in all_players: for player_tables in all_tables: # "for player_tables" loop for version in player_tables: # things have gone wrong, go to next iteration of all_sports loop break else: continue break else: continue break That uses the else clause for loops, which will execute if no break is used in the loop, thus the loop runs to completion.
🌐
H2K Infosys
h2kinfosys.com › blog › python break, continue and pass statements in loops
Python Break, Continue, and Pass Statements: The Ultimate Guide
January 5, 2026 - In Python, the Python Break statement ... languages such as Java or PHP, Python does not support labeled continue statements that allow you to skip directly to the next iteration of an outer loop....
🌐
AskPython
askpython.com › home › python continue statement
Python continue Statement - AskPython
July 7, 2022 - Python continue statement is used to skip the execution of the current iteration of the loop. Python continue vs break, continue vs pass statement in Python.
Find elsewhere
🌐
Verve AI
vervecopilot.com › interview-questions › how-can-you-truly-master-python-continue-outer-loop-for-peak-interview-performance
How Can You Truly Master Python Continue Outer Loop For Peak Interview… · Loop For Peak Interview Performance · Interview Q&A | Verve AI
August 28, 2025 - When you have nested loops—a loop inside another loop—the `continue` statement only affects the innermost loop in which it is placed [^3]. It does not "bubble up" to control an enclosing loop. ```python for i in range(3): # Outer loop for j in range(3): # Inner loop if j == 1: continue # This 'continue' only affects the inner loop (j) print(f"i: {i}, j: {j}")
🌐
Programming Idioms
programming-idioms.org › idiom › 42 › continue-outer-loop › 1264 › python
Continue outer loop, in Python
February 18, 2016 - auto a = [1,2,3,4,5]; auto b = [3,5]; void main() { mainloop: foreach(v; a){ foreach(w; b){ if(v == w) continue mainloop; } writeln(v); } } ... main() { var a = [1, 2, 3]; var b = [2, 3]; outer: for (var v in a) { for (var w in b) { if (w == v) continue outer; } print(v); } }
🌐
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 ... The code with explanation is as follows. for i in l1: print('Start outer loop') for j in l2: print('--', i, j) if i == 2 and j == 20: print('-- BREAK inner loop') break else: print('-- Finish inner loop without BREAK') continue print('BREAK outer loop') break # Start outer loop # -- 1 10 # -- 1 20 # -- 1 30 # -- Finish inner loop without BREAK # Start outer loop # -- 2 10 # -- 2 20 # -- BREAK inner loop # BREAK outer loop
🌐
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 - Sometimes, you need to exit multiple levels of loops at once when a certain condition is met, such as finding a match or encountering invalid data. Python doesn’t have a built-in break outer like some languages, so you’ll often use techniques like flags, functions with return, or exceptions to exit nested loops cleanly.
🌐
PYnative
pynative.com › home › python › nested loops in python
Python Nested Loops [With Examples] – PYnative
September 2, 2021 - If the outer number and the inner loop’s current number are the same, then move to the next iteration of an inner loop. ... first = [2, 4, 6] second = [2, 4, 6] for i in first: for j in second: if i == j: continue print(i, '*', j, '= ', i * j)Code language: Python (python) Run
🌐
Reddit
reddit.com › r/cs50 › tideman, how to break out of a nested inner loop but continue the outer loop
r/cs50 on Reddit: tideman, how to break out of a nested inner loop but continue the outer loop
November 29, 2022 -

This is my first time posting a coding question on any website, so apologies if i dont do a great job. Constructive feedback is very welcome.

I cant figure out a way to break out of the inner nested loop but continue the outer loop. As in, if is_cycle is true, the lines:

locked[pairs[i].winner][pairs[i].loser] = true; 
num_locked++; 

need to be skipped for that current iteration of the outer loop.

Thank you so much.

// Lock pairs into the candidate graph in order, without creating cycles 
void lock_pairs(void) 
{     
    int num_locked = 0;     

    //loop through pairs 
        //has loser won before? 
            //if no, lock the pair 
            //if yes, call is_cycle on pair. if its not a cycle lock the pair 
    for (int i = 0; i < pair_count; i++)     
    {        
        //has the loser won before? 
        for (int j = 0; j < i; j++)         
        {             
            if (pairs[i].loser == pairs[j].winner)             
            {                 
                //if the loser has won before and it creates a cycle, break the inner
                //loop, continue outer 
                    if (is_cycle(pairs[i], pairs[j], num_locked))                 
                    {                     
                        break;                 
                    }             
            }         
        }  
       
        //this is incorrect this will lock the pair each time         
        locked[pairs[i].winner][pairs[i].loser] = true;         
        num_locked++;     
        } 
     
    return; 
} 

I have tried searching through stack overflow. Some mentioned a goto
function but most people said that is bad programming. someone else mentioned creating a separate function and using return
statements but i need that outer loop to continue, not stop. And one other answer suggested using flags
, which after more searching i still dont get how that could help.

🌐
Codefinity
codefinity.com › courses › v2 › e75c55c0-bf61-4b3f-b359-65a0786e7ed0 › 734866f8-39d5-fb17-a80f-ffb9fcb6f22e › e4405758-786d-49cd-b865-4d89a9e76922
Learn Break and Continue with Nested Loops | Nested Loops
12345678 for i in range(10): print('Outer loop starts!') for j in range(10): print(i, j) if i==j: print('Leaving inner loop') break print('Outer loop is over!') ... The same about continue: you'll go back to the beginning of the inner loop.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-break-out-of-multiple-loops-in-python
How to Break out of multiple loops in Python ? - GeeksforGeeks
February 24, 2023 - ... # Python code to break out ... print('Element found') break else: print(j) # If the inner loop terminates due to # the break statement and flag is True # then the following if block will # be executed and the break statement ...
🌐
Flexiple
flexiple.com › python › python-continue-statement
Python Continue Statement - Flexiple
March 14, 2024 - The Python continue statement is a control flow tool used within loops. When encountered, it immediately ends the current iteration and proceeds to the next one, skipping any remaining code in the loop's body.
🌐
Iditect
iditect.com › faq › python › python-continuing-to-next-iteration-in-outer-loop.html
Python: Continuing to next iteration in outer loop
You can label the outer loop and use the continue statement with the label to continue to the next iteration of the outer loop from within the inner loop.