DEV Community

Cover image for How to fix "SyntaxError: ‘break’ outside loop" in Python
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

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

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

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.

Here’s what the error looks like:

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

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Thanks for reading.

❤️ You might like:

Top comments (0)