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

Updated Sep 22, 2023 ⤳ 3 min read

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

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.

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

How to fix SyntaxError: 'break' outside loop

This syntax error 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: 


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.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`
Exit mobile version