The continue statement in Python is one of the most useful control flow tools for managing loops. It allows developers to skip the rest of the code inside the current iteration and jump directly to the next one. This behavior helps you manage complex looping logic more efficiently, especially when you want to avoid certain conditions or values during iteration.

What Is the Continue Statement?
In Python, loops are essential structures for repeating a block of code multiple times. There are two main types of loops — for loops and while loops. The continue statement provides additional control by allowing you to skip an iteration without terminating the loop completely.
Here’s the basic syntax of a continue statement inside a Python loop:
for item in iterable:
if condition:
continue
# code to execute if condition is False
When Python encounters the continue keyword, it immediately skips to the next iteration of the loop, ignoring any remaining statements inside the current loop block.
Continue Statement in a For Loop
Let’s see an example of how the continue statement works inside a for loop. Suppose you want to print all numbers from 1 to 10, but you want to skip the even numbers.
for i in range(1, 11):
if i % 2 == 0:
continue
print(i)
Output:
1 3 5 7 9
Explanation: In the example above, when i is even (like 2, 4, 6, 8, 10), the continue statement skips the print() function and goes directly to the next iteration.
Continue Statement in a While Loop
The continue statement also works in a while loop. Let’s say you want to count from 0 to 10, but skip all numbers divisible by 3.
i = 0
while i <= 10:
i += 1
if i % 3 == 0:
continue
print(i)
Output:
1 2 4 5 7 8 10 11
In this case, whenever i is divisible by 3, the loop skips the print(i) statement and moves to the next iteration. Notice that we increment i before the continue statement — this is very important to prevent infinite loops.
Common Mistake: Forgetting to Update the Loop Variable
When using continue inside a while loop, you must be careful to update the loop variable before the continue statement. Forgetting to do so can lead to infinite loops. Consider the following incorrect example:
i = 0
while i < 5:
if i == 2:
continue # i never increments here
print(i)
i += 1
This code will create an infinite loop because when i == 2, the loop keeps repeating without incrementing i. To fix it, move i += 1 before the continue statement.
Practical Example: Skipping Negative Numbers in a List
Here’s a real-world use case of the continue statement. Suppose you are processing a list of numbers that includes both positive and negative values, and you only want to calculate the sum of positive numbers.
numbers = [10, -3, 5, -7, 9, -2]
positive_sum = 0
for num in numbers:
if num < 0:
continue
positive_sum += num
print("Total positive sum:", positive_sum)
Output:
Total positive sum: 24
In this scenario, every time a negative number appears, the loop uses continue to skip it and move on to the next number. This helps you filter out unwanted data without adding complex conditional nesting.
Continue vs Break vs Pass
Python offers three important control flow statements for managing loops: break, continue, and pass. The table below compares them.
| Statement | Description | Effect |
|---|---|---|
| break | Immediately exits the loop | Terminates the entire loop |
| continue | Skips the rest of the current iteration | Moves to the next iteration |
| pass | Does nothing; a placeholder statement | Executes but has no effect |
Nested Loops with Continue
You can also use continue inside nested loops. It will only affect the innermost loop it is placed in.
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
continue
print(f"i={i}, j={j}")
Output:
i=1, j=1 i=1, j=3 i=2, j=1 i=2, j=3 i=3, j=1 i=3, j=3
Here, when j == 2, the inner loop skips printing and moves to the next iteration. The outer loop continues as usual.
When to Use Continue Statement
- When you need to skip certain items in a loop based on conditions.
- To make code cleaner and avoid unnecessary nested
ifstatements. - When filtering or processing large data sets with specific rules.
Conclusion
The continue statement in Python is a powerful control flow feature that helps make loops more efficient and readable. By skipping specific iterations, you can simplify your logic and improve performance in both for and while loops.
Mastering loop control statements like continue, break, and pass is essential for writing clean and optimized Python programs.
For more details about Python’s official syntax, visit the Python documentation on continue statements.