Syntaxerror: ‘continue’ not properly in loop

The syntaxerror: ‘continue’ not properly in loop is a common error message usually encounter while using loop structure in your Python code.

This error is not hard to fix as you think, but in order to resolve this error.

We need a proper understanding of how to fix the syntaxerror continue not properly in loop error.

Fortunately, this article will walk you through troubleshooting this error, so let’s get started!

What is “syntaxerror continue not properly in loop”?

The syntaxerror: ‘continue’ not properly in loop occurs when you’re trying to use the continue statement outside of a loop.

It usually happens when the Python interpreter encounters the continue statement outside the loop.

For example:

def sample():
    a = 10
    if a == 10:
        continue

sample()

When we try to run this sample code, it will result in a SyntaxError.

File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 4
    continue
    ^^^^^^^^
SyntaxError: 'continue' not properly in loop

Note: Continue and break works with for loop and while loop. The break statement terminates the loop. Meanwhile, the continue statement skips one iteration.

What is continue statement in Python?

The continue statement is used in loops to skip the rest of the code inside a loop for the current iteration and move to the next iteration of the loop.

Unfortunately, if it is not placed within a loop or is misaligned, the interpreter throws SyntaxError.

Why does “continue not properly in loop” error occurs in Python?

This error can occur due to some reason, such as:

👎 Incorrect usage of the continue statement is one of the major causes of why this error keeps on bothering you. The continue statement can only be used, within the body of a loop, such as a for or while loop.

👎 When the continue statement is placed outside of a loop or within a different block of code, this error will occur.

👎 Another thing is when you used the continue statement in the if block, that’s not part of a loop.

How to fix the “syntaxerror: ‘continue’ not properly in loop”?

To fix the syntaxerror continue not properly in loop error, ensure that the continue statement is used within a loop (for, while). Also, check the indentions of your code.

Here are the following solution, which you can vary pin order to resolve this error.

Solution 1: Put the continue statement inside the Loop

By making sure that the continue statement is placed within the loop structure, we can fix the error.

Incorrect code:

def sample():
    a = 10
    if a == 10:
        continue

sample()

Corrected code:

def sample():
    for a in range(10):
        if a == 10:
            continue
        print(a)

sample()

As you can see, the continue statement is used inside a for loop. When the value of a is equal to 10, the continue statement is executed and the rest of the code inside the loop is skipped for that iteration.

This ensures that the continue statement is used properly within the loop, eliminating the syntax error.

Output:

0
1
2
3
4
5
6
7
8
9

Here’s an example code using while loop:

def sample():
    a = 0
    while a < 10:
        if a == 10:
            a += 1
            continue
        print(a)
        a += 1

sample()

Output:

0
1
2
3
4
5
6
7
8
9

Solution 2: Remove the continue statement and replace it with an if statement

For example:

def sample():
    for a in range(3):
        if a != 3:
            print(a)

sample()

Rather than using a continue statement to skip printing the value of a when it is equal to 3, we use an if statement to only print the value of a when it is not equal to 3.

Output:

0
1
2

Solution 3: Remove the continue statement and replace iit with a pass statement

You can fix the error by replacing the continue statement with a pass statement. The pass statement does nothing and is used as a placeholder when you need a statement in your code.

For example:

def sample():
    for a in range(8):
        if a == 5:
            pass
        else:
            print(a)

sample()


In this example, the value of a is equal to 8, the pass statement is executed and no error appears.

Output:

0
1
2
3
4
6
7

Solution 5: Use proper indention

Ensure your code in the loop has been indented appropriately.

Incorrect code:

for a in range(15):
if a % 3 == 0:
      continue

    print(a)

Python interpreter can’t read your code if it is indented incorrectly.

You can use the tab to correct the indention of your code.

Corrected code:

for a in range(15):
    if a % 3 == 0:
      continue

    print(a)

Output:

1
2
4
5
7
8
10
11
13
14

Conclusion

In conclusion, the error message syntaxerror: ‘continue’ not properly in loop occurs when you’re trying to use the continue statement outside of a loop.

It usually happens when the Python interpreter encounters the continue statement outside the loop.

To fix the syntaxerror continue not properly in loop error, ensure that the continue statement is used within a loop (for, while). Also, check the indentions of your code.

This error message is easy to fix and understand, by executing the solutions above, you can master this SyntaxError with the help of this guide.

You could also check out other SyntaxError articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Frequently Asked Questions

What is Python SyntaxError and what causes it?

SyntaxError is raised when Python’s parser can’t understand your code. Common causes: missing colon (def foo() instead of def foo():), unmatched parentheses or brackets, incorrect indentation, mixing tabs and spaces, missing comma in a list, or using a Python 3 feature in a Python 2 interpreter (or vice versa). The error points to a line, but the actual issue is often on the line BEFORE.

How do I fix ‘unexpected EOF while parsing’?

Python ran out of code while expecting more, usually unclosed parenthesis, bracket, brace, or quote. Scroll up from the error line and count opening vs closing pairs. Use an IDE with bracket matching (VS Code, PyCharm) to highlight pairs. Common gotcha: a triple-quoted string that’s missing its closing triple-quote.

Why does my syntax error point to a line that looks correct?

The actual error is often on the line BEFORE the one Python reports. Python reads code until something doesn’t parse, then reports the position where it gave up, not where the user’s mistake started. Check the line above the reported error for missing colons, commas, or closing brackets.

Why does ‘print x’ raise SyntaxError in Python 3?

Python 3 made print a function, so print(x) is required. The bare ‘print x’ syntax was Python 2 only. If you’re following an old tutorial, check whether it targets Python 2 (released 2008) or Python 3 (your current interpreter). Update all print statements to print(…) function calls.

Where can I find more SyntaxError fixes?

Browse the SyntaxError reference hub for 48+ specific fixes (Python and JavaScript). For Python fundamentals see the Python Tutorial hub. For JavaScript syntax errors see the JavaScript Tutorial hub.

Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment