Because break cannot be used to break out of an if statement -it can only break out of loops. That's the way Python (and most other languages) are specified to behave.

What are you trying to do? Perhaps you should use sys.exit() or return instead?

Answer from Mark Byers on Stack Overflow
Top answer
1 of 6
102

Because break cannot be used to break out of an if statement -it can only break out of loops. That's the way Python (and most other languages) are specified to behave.

What are you trying to do? Perhaps you should use sys.exit() or return instead?

2 of 6
6

Some other common ways this error may show up:

1. Incorrect indentation

Indentation is extremely important in Python. It helps determine the grouping of statements, so if you don't group statements correctly, your code will not work as expected.

Suppose you perform some computation inside a loop and depending on its result, either continue on in the loop or break out of the loop. If you inadvertently put the if-else block outside the loop because you missed the indentation, you will get the error in the title. To solve the error in that case, use indentation correctly.

For example the following will raise the error in the title.

for i in range(10):
    print(i)        # <--- do something
if i % 3:           # <--- break out of loop on some condition
    break

The following solves this error:

for i in range(10):
    print(i)
    if i % 3:       # <--- use indentation correctly
        break       # <--- to break out of loop

Of course your program will probably have much more code than this but the general structure might be the same in which case, see if the proposed solution applies.

I think this issue is exacerbated by some IDEs that use 2 spaces (not the 4 recommended by PEP8) per indentation level by default, which sometimes makes it difficult to see where indentation ends.

2. Include a break statement inside a function

The break statement must be explicitly inside the loop definition; it cannot be nested in a function that will be executed inside a loop.

For example, the following raises the error in the title:

def func(x):
    x *= 10
    if x > 20:     # <--- break out of loop on condition
        break

for i in range(10):
    func(i)        # <--- call function

To solve it, move the conditional check out of the function and put it in the loop.

def func(x):
    x *= 10
    return x       # <--- return the value

for i in range(10):
    x = func(i)    # <--- call function
    if x > 20:     # <--- check condition
        break
Discussions

i am getting an error saying "break outside loop"
sai jayanth kashyap is having issues with: while executing the same program i am getting an error saying break outside loop. what does that suppose to mean? am i intending the break incor... More on teamtreehouse.com
🌐 teamtreehouse.com
3
November 4, 2016
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
error : break outside loop
what do you expect to happen when the "break" line is reached? In general break can only be used inside a loop, and return inside a function. If you want to stop your code in a different situation, just let it run to end. (Skipping whatever parts you don't want to execute with the help of if conditions) More on reddit.com
🌐 r/learnpython
12
0
June 1, 2023
break outside of loop?
You cannot break outside a loop… and, really, the idea of doing so doesn’t make any sense. What you probably want to do is define a function that does what you want to do, and then inside a loop call that function again as many times as needed. More on reddit.com
🌐 r/learnpython
13
2
September 4, 2022
🌐
Career Karma
careerkarma.com › blog › python › python syntaxerror: ‘break’ outside loop solution
Python SyntaxError: ‘break’ outside loop Solution | Career Karma
December 1, 2023 - A break statement instructs Python to exit a loop. If you use a break statement outside of a loop, for instance, in an if statement without a parent loop, you’ll encounter the “SyntaxError: ‘break’ outside loop” error in your code.
🌐
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.
🌐
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): ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-break-statement
Python Break Statement - GeeksforGeeks
The break statement can be used within a for loop to exit the loop before it has iterated over all items, based on a specified condition. ... A while loop in Python repeatedly executes a block of code as long as a specified condition is True.
Published   June 5, 2026
🌐
Team Treehouse
teamtreehouse.com › community › i-am-getting-an-error-saying-break-outside-loop
i am getting an error saying "break outside loop" (Example) | Treehouse Community
November 4, 2016 - print("Enter your today schedule") print("Enter Done when done with scheduling") todo_list = [] while True: list = input("> ") todo_list.append(list) if input == 'done': break print(" Here is your all day schedule") for todo in todo_list: ...
Find elsewhere
🌐
DEV Community
dev.to › lavary › how-to-fix-syntaxerror-break-outside-loop-in-python-3o3m
How to fix "SyntaxError: ‘break’ outside loop" in Python - DEV Community
February 2, 2023 - Based on Python syntax, the break keyword is only valid inside loops - for and while. ... values = [7, 8, 9.5, 12] for i in values: # Break out of the loop if i > 10 if (i > 10): break print(i) The above code iterates over a list and prints out the values less than 10. Once it reaches a value greater than 10, it breaks out of the loop. The error "SyntaxError: 'break' outside loop" occurs under two scenarios:
🌐
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 ...
🌐
Quora
quora.com › What-does-break-outside-Loop-mean-in-Python
What does break outside Loop mean in Python? - Quora
Answer (1 of 4): The break statement can only be used in side either a for loop or a while loop. The break outside Loop error means that your code has a break statement that is not inside a loop. The most common cause of this error is indentation - you have a break statement which you think is ...
🌐
Python
wiki.python.org › moin › Asking(20)for(20)Help(2f27)break(2720)outside(20)loop.html
Asking for Help/'break' outside loop
It sounds like the break statement isn't being associated with the statements inside the loop because it isn't at the right indentation level. Python uses "whitespace" - that is, spaces and tabs, which effectively leave blank space in a line of a program - to indicate groups of statements.
🌐
Taggart-tech
taggart-tech.com › reckoning
I used AI. It worked. I hated it.: Taggart Tech
Yeah, that's how you get got in this process. Once you stop scrutinizing the model's output, the probability something goes off the rails approaches 1. "Human in the loop" is necessary, but the current process itself makes the loop stultifying, and encourages the human to take themselves out of the loop.
🌐
GitConnected
levelup.gitconnected.com › beyond-vibe-coding-building-an-agentic-engineering-team-adc0283d1937
Beyond Vibe Coding: Building an Agentic Engineering Team | by Christopher Montes | Level Up Coding
April 1, 2026 - Then I introduced Ive, for a pass at the UX of the human-in-the-loop component. Ive came back with twelve gaps the discovery had missed. What happens when the user hits Ctrl+C mid-training? How does error recovery work across three different flow types? What’s the partial-save behavior?
🌐
Enki
enki.com › post › how-to-exit-a-loop-in-python
Enki | Blog - How to Exit a Loop in Python
While loops run as long as a specific ... need, or we've met another condition that makes further iterations unnecessary. The break statement helps us terminate or exit a loop before its natural end....
🌐
Educative
educative.io › answers › how-to-break-out-of-a-loop-in-python
How to break out of a loop in Python
A break statement in Python exits out of a loop. This statement is used under your loop statement, usually after a conditional if statement.
🌐
Itsourcecode
itsourcecode.com › home › syntaxerror: ‘break’ outside loop
Syntaxerror: 'break' outside loop [SOLVED]
May 22, 2023 - No, a break statement can only be used inside of a loop and cannot be used outside of a loop construct.
🌐
Codingdeeply
codingdeeply.com › home › break outside loop python: avoiding common mistakes & solutions
Break Outside Loop Python: Avoiding Common Mistakes & Solutions
February 23, 2024 - The break statement is used to exit a loop in Python prematurely. Placing the break statement outside of a loop structure will result in the “break is outside loop python” error.
🌐
W3Schools
w3schools.com › c › c_break_continue.php
C Break and Continue
You can also combine break and continue. This example skips printing 2 and stops the loop at 4:
🌐
Realpython
static.realpython.com › python-cheatsheet.pdf pdf
Real Python Pocket Reference Visit realpython.com to turbocharge your
print("You can go outside.") Learn ... · Loops · range(5) generates 0 through 4 · Use enumerate() to get index and value · break exits the loop, continue skips to next ·...