How to fix "SyntaxError: ‘break’ outside loop" in Python
Tags: python, programming
Published: 2023-02-02 20:38:36

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/python-syntaxerror-break-outside-loop-fixed) 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
```

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:

```python
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. <u>When using `break` inside an `if` block that's not part of a loop</u>
2. <u>When using `break` (instead of `return`) to return from a function</u>

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:

```python
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:

```python
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:

```python
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):

```python
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:**

- [TabError: inconsistent use of tabs and spaces in indentation (Python)](https://www.decodingweb.dev/python-taberror-inconsistent-use-of-tabs-and-spaces-in-indentation-fix)
- [How to reverse a range in Python (with examples)](https://www.decodingweb.dev/reverse-a-range-in-python)
- [Unindent does not match any outer indentation level error in Python (Fixed)](https://www.decodingweb.dev/python-indentationerror-unindent-does-not-match-any-outer-indentation-level-fix)