How to fix "SyntaxError: ‘continue’ not properly in loop" in Python
Tags: pyth, programming
Published: 2023-02-02 20:21:14

> ✋ **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-continue-not-properly-in-loop-fix) for a 💯 user experience. _~reza_

Python raises “SyntaxError: ‘continue’ not properly in loop” whenever it encounters a continue statement outside a loop – usually within an if block that’s not part of a loop.

Here’s what the error looks like:


```python
File /dwd/sandbox/test.py, line 8
    continue
    ^^^^^^^^
SyntaxError: 'continue' not properly in loop
```

A `continue` statement is a control flow feature used within a loop to skip the rest of the current iteration and continue to the beginning of the next one.

The difference between `continue` and `break` is that a `break` statement terminates the loop while `continue` skips one iteration.

You usually use continue when you reach a specific value and you want to skip the rest of the iteration and proceed to the next iteration. That's pretty much like the C language.

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

In the following example, we iterate over a list of scores and print those above ⭐ 4.5 (inclusive):

```python
scores = [3.5, 3.8, 4.6, 4.5, 4.9, 3.9, 5, 1.2, 3, 4, 4.6]
top_scores = []

for score in scores:
    if (score <= 4.5):
        continue
    
    top_scores.append(score)

print(top_scores)
# Output: [4.6, 4.9, 5, 4.6]
```

In the above code, if the `score` value in the current iteration is less than `4.5`, we continue to the next iteration.

## How to fix SyntaxError: 'continue' not properly in loop

One of the most common causes of "SyntaxError: 'continue' not properly in loop" is using the `continue` keyword in an `if` block that's not part of a loop:

```python
user = {'id': 2, 'is_active': True}

if user:
    if is_active != True:
        continue # 🚫 SyntaxError
```

There's no point in using a `continue` statement within an `if` block. If the condition isn't met, the code isn't executed anyway. The above code would only make sense if it's inside a loop:

```python
users = [
    {'id': 1, 'is_active': True},
    {'id': 2, 'is_active': False},
    {'id': 3, 'is_active': True},
]

for user in users:
    if user['is_active'] == False:
        continue
    
    print(f'Sending an email to user {user[id]}')
    # Some code here ...

# Output: 
# Sending an email to 1
# Sending an email to 3
```

If you want to keep the if block for syntactical reasons, you can replace the continue 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.

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

Thanks for reading.

❤️ **You might like:**

- [SyntaxError: 'break' outside loop in Python (Solved)](https://www.decodingweb.dev/python-syntaxerror-break-outside-loop-fixed)
- [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)