SyntaxError: 'continue' not properly in loop in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# SyntaxError: 'continue' not properly in loop in Python

The Python "SyntaxError: 'continue' not properly in loop" occurs when we use the continue statement outside of a loop.

To solve the error, use the continue statement in a for or a while loop and make sure your code is indented correctly.

syntaxerror continue not properly in loop

# Using the continue statement outside a loop

Here is an example of how the error occurs.

main.py
if len('hi') == 2: # โ›”๏ธ SyntaxError: 'continue' not properly in loop continue

using continue statement outside loop

Using the continue statement outside a for or a while loop causes the error.

The continue statement can be used to continue to the next iteration of a for or a while loop.

main.py
for i in range(10): if i % 2 == 0: continue print(i) # ๐Ÿ‘‰๏ธ 1, 3, 5, 7, 9

use continue in for or while loop

The example above checks if each number in the range is divisible by 2 and if it is, we continue (skip) to the next iteration.

# Using the continue statement in a while loop

Here is an example that uses a continue statement in a while loop.

main.py
num = 0 while True: num += 1 if num > 10: break if num % 2 == 0: continue print(num) # ๐Ÿ‘‰๏ธ 1, 3, 5, 7, 9

using continue statement in while loop

The while loop iterates until its condition returns false or the break statement is used.

The break statement breaks out of the innermost enclosing for or while loop.

Whereas, the continue statement continues to the next iteration of the loop.

# Make sure to indent your code in the loop correctly

Make sure you have indented your code in the loop correctly.

main.py
for i in range(10): if i % 2 == 0: # โ›”๏ธ Bad indentation continue print(i) # ๐Ÿ‘‰๏ธ 1, 3, 5, 7, 9

For the Python interpreter to be able to read your code, it has to be indented correctly.

Use tabs to indent the code as shown in the following code sample.

main.py
for i in range(10): if i % 2 == 0: # โœ… Proper indentation continue print(i) # ๐Ÿ‘‰๏ธ 1, 3, 5, 7, 9
If the code that uses the continue statement is not indented consistently, the interpreter won't recognize it as part of the loop.

You should either indent your code using tabs or using spaces.

However, you should never mix the 2 because mixing tabs and spaces in the same code block often causes issues in Python.

# Using a pass statement to implement the block later

If you need to handle a block of code that's not yet implemented, use the pass statement.

main.py
if len('hi') == 2: pass

The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.

# Use a return statement to return a value from a function

If you meant to return a value from a function, use the return statement.

main.py
def get_name(): return 'Bobby Hadz' name = get_name() print(name) # ๐Ÿ‘‰๏ธ 'Bobby Hadz'

The return statement can be used to return a value and exit the function.

If you don't need to return a specific value and just need to exit the function, simply return.

# How the continue statement works in Python

The continue statement can only be used in a for or a while loop.

The statement cannot be used in a function or a class that is within a loop.

The continue statement continues to the next iteration of the enclosing loop.

main.py
for i in range(10): if i % 2 == 0: continue print(i) # ๐Ÿ‘‰๏ธ 1, 3, 5, 7, 9

On each iteration where the i variable stores a number that is divisible by 2, a continue statement is used to skip to the next iteration of the loop.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev