Skip to main content

Command Palette

Search for a command to run...

How to fix "SyntaxError: ‘break’ outside loop" in Python

Updated
•3 min read•View as Markdown
How to fix "SyntaxError: ‘break’ outside loop" in Python
R

Hey folks 👋 My name's Reza, I'm a developer & author who enjoys simplifying the complex side of tech. I also make tiny infographics about coding, web performance and SEO; get them on my Twitter: @rlavarian

Python raises “SyntaxError: ‘break’ outside loop” whenever it encounters a break statement outside a loop. The most common cases are using break within an if block (that’s not part of a loop) or when you accidentally use it instead of return to return from a function.

✋ Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

Here’s what the error looks like:

File /dwd/sandbox/test.py, line 2
  break
  ^^^^^
SyntaxError: 'break' outside loop

The break statement is a control flow feature used to break out of the innermost loop. For instance, when you reach a specific value. That's pretty much like the C language.

Based on Python syntax, the break keyword is only valid inside loops - for and while.

Here's an example:

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.

How to fix SyntaxError: 'break' outside loop

The error "SyntaxError: 'break' outside loop" occurs under two scenarios:

  1. When using break inside an if block that's not part of a loop

  2. When using break (instead of return) to return from a function

Let's see some examples with their solutions.

When using break inside an if block that's not part of a loop: One of the most common causes of "SyntaxError: 'break' outside loop" is using the break keyword in an if block that's not part of a loop:

if item > 100
  break # 🚫 SyntaxError: 'break' outside loop

# some code here

There's no point in breaking out of an if block. If the condition isn't met, the code isn't executed anyway. The above code only would make sense if it's inside a loop:

values = [7, 8, 9.5, 12]

for item in values:
    if (item > 10):
        break
    print(i)

Otherwise, it'll be useless while being a SyntaxError too!

However, if you want to keep the if block for syntactical reasons, you can replace the break keyword with the pass keyword.

A pass statement does nothing in Python. However, you can always use it when a statement is required syntactically, but no action is needed.

When using break (instead of return) to return from a function: Another reason behind this error is to accidentally use the break keyword (instead of return) to return from a function:

def checkAge(age):
    if (age < 12):
        break # 🚫 SyntaxError: 'break' outside loop

    # some code here

To return from a function, you should always use return (with or without a value):

def checkAge(age):
    if (age <= 12):
        return

    # some code here

Alright, I think it does it. I hope this quick guide helped you solve your problem.

Thanks for reading.

❤️ You might like:

More from this blog

Reza Lavarian

52 posts

Hey 👋 My name's Reza, I'm a developer & author who enjoys simplifying the complex side of tech. I also make tiny infographics about coding, performance & SEO.