Skip to main content

How to Solve "SyntaxError: 'continue' not properly in loop" in Python

The SyntaxError: 'continue' not properly in loop error in Python occurs when the continue statement is used outside of a for or while loop.

This guide explains why this error occurs and provides clear solutions, focusing on correct code structure and indentation.

Understanding the continue Statement

The continue statement is a loop control statement. Its purpose is to immediately jump to the next iteration of the innermost enclosing loop (either a for loop or a while loop), skipping any remaining code within the current iteration of the loop's body.

Correct Example: In this example, when i is even, the continue statement is executed. The print(i) line is skipped, and the loop immediately goes to the next value of i.

for i in range(10):
if i % 2 == 0: # If 'i' is even
continue # Skip the rest of this iteration, go to the next 'i'
print(i) # Only odd numbers will be printed

Output:

1
3
5
7
9

Common Causes and Solutions

continue Outside a Loop

The most common cause is simply placing a continue statement outside of any loop:

if len('hi') == 2:
# ⛔️ SyntaxError: 'continue' not properly in loop
continue # WRONG: 'continue' must be inside a loop

Solution: Ensure that the continue statement is inside the body of a for or while loop:

for i in range(5): # Example using a for loop
if i == 2:
continue # Correct: Inside the loop
print(i) # Output: 0 1 3 4

Incorrect Indentation

Incorrect indentation can appear to place a continue statement outside a loop, even if it's visually close to the loop:

for i in range(10):
if i % 2 == 0:
continue # Correct indentation

print(i)

# ⛔️ SyntaxError: 'continue' not properly in loop
#for i in range(10):
#if i % 2 == 0: # Incorrect indentation
#continue

Solution: Carefully check your indentation. In Python, indentation defines code blocks. The continue statement must be indented to be inside the loop's body. Use a consistent indentation style (typically 4 spaces, as recommended by PEP 8).

continue vs. break vs. pass vs. return

It's important to understand the differences between these related keywords:

  • continue: Skips the rest of the current iteration and goes to the next iteration of the loop.
  • break: Exits the loop entirely. No further iterations will occur.
  • pass: A no-operation statement. It does nothing. It's used as a placeholder where syntactically some code is required, but you don't want to do anything yet. It does not affect loop control.
  • return: Exits the current function. If the return is inside a loop within that function, it will also exit the loop, but more importantly, it exits the entire function.
for i in range(3):
print(f"Loop start. i={i}")
if i == 1:
print("i is 1, continuing")
continue
print("This line is skipped when i is 1")

Output:

Loop start. i=0
This line is skipped when i is 1
Loop start. i=1
i is 1, continuing
Loop start. i=2
This line is skipped when i is 1
for i in range(3):
print(f"Loop start. i={i}")
if i == 1:
print("i is 1, breaking")
break
print("This line is skipped when i is 1")

Output:

Loop start. i=0
This line is skipped when i is 1
Loop start. i=1
i is 1, breaking
for i in range(3):
print(f"Loop start. i={i}")
if i == 1:
print("i is 1, passing")
pass # Does nothing!
print("This line is ALWAYS printed")

Output:

Loop start. i=0
This line is ALWAYS printed
Loop start. i=1
i is 1, passing
This line is ALWAYS printed
Loop start. i=2
This line is ALWAYS printed
def my_func():
for i in range(3):
print(f"Loop start. i={i}")
if i == 1:
print("i is 1, returning")
return # Exits the function AND the loop
print("This line is skipped when i is 1")

my_func()

Output:

Loop start. i=0
This line is skipped when i is 1
Loop start. i=1
i is 1, returning
  • Note the difference in outputs for these 4 keywords.

Conclusion

The SyntaxError: 'continue' not properly in loop error is always caused by using continue outside of a for or while loop. Carefully check your code's structure and indentation to ensure that continue is placed correctly within the loop's body.

Understand the differences between continue, break, pass and return to use the appropriate control flow statement for your needs.