Back to Blog
Python

Python Continue Statement: Skip Loop Iterations

python continue statement: Learn how the Python continue statement works, when to use it, and how it differs from break and pass in loops.

Python loopscontinue statementcontrol flowfor loopwhile loop
Illustration of a Python loop with a continue statement skipping an iteration

The python continue statement skips the rest of the current iteration and moves the loop to the next item. It is one of the most direct ways to filter out unwanted cases without breaking the loop entirely. In a for loop, continue jumps to the next element; in a while loop, it jumps to the next condition check. This behavior makes it a core tool for loop control flow in Python.

How the Continue Statement Works

When the interpreter encounters continue inside a loop, it immediately stops executing the remaining code in the current iteration and proceeds to the next iteration. For a for loop, that means the next element from the iterable is retrieved. For a while loop, the condition is evaluated again before the next iteration begins.

for number in range(10): if number % 2 == 0: continue print(number)

This code prints only odd numbers from 0 to 9. When number is even, continue is executed, so the print call is skipped and the loop moves to the next value. The key detail is that continue does not terminate the loop; it only ends the current pass through the loop body.

Continue vs. Break vs. Pass

Python provides three loop-control keywords that are often confused: continue, break, and pass. Each has a distinct purpose.

KeywordBehaviorTypical Use
continueSkips the rest of the current iterationFiltering out specific cases
breakExits the entire loop immediatelyStopping when a condition is met
passDoes nothing; acts as a placeholderStub code that must be syntactically valid

pass is not a loop-control statement in the same sense. It simply occupies a line where Python requires a statement. The following example shows all three in context:

for item in [1, 2, 3, 4, 5]: if item == 3: break if item == 2: continue if item == 1: pass print(item)

The loop breaks at 3, skips printing 2, and prints 1 because pass has no effect. Understanding these differences prevents accidental early termination or silent no-ops.

Using Continue to Skip Unwanted Iterations

The most common use of continue is to skip items that do not meet a condition. This is especially useful when the loop body is long and you want to avoid deeply nested if statements.

for user in users: if not user.is_active: continue if user.email is None: continue send_newsletter(user.email)

Without continue, the same logic would require nested conditionals:

for user in users: if user.is_active and user.email is not None: send_newsletter(user.email)

Both approaches are valid, but continue keeps the main processing at the top level of the loop body. This is a readability win when there are multiple independent validation steps. It also makes it easier to add or remove filters without restructuring the entire loop.

Continue in Nested Loops

In nested loops, continue applies only to the innermost loop that contains it. It does not affect the outer loop. This is a common source of confusion for developers coming from languages with labeled breaks or continues.

for row in matrix: for value in row: if value < 0: continue print(value)

Here, negative values are skipped in the inner loop, but the outer loop continues to the next row. If you need to skip an iteration of the outer loop based on an inner condition, you must use a flag or restructure the logic.

for row in matrix: if any(value < 0 for value in row): continue process_row(row)

This uses a generator expression to check the entire row before deciding whether to skip it. The continue now applies to the outer loop because it appears directly in that loop's body.

Common Mistakes with Continue

A frequent mistake is using continue in a while loop without updating the loop variable first. Because continue jumps to the next condition check, any increment placed after continue is skipped, leading to an infinite loop.

counter = 0 while counter < 10: if counter == 5: continue # counter never increments print(counter) counter += 1

This code will print 0 through 4, then loop forever when counter is 5 because the increment is never reached. The fix is to increment before the continue or to use a for loop instead.

Another mistake is using continue inside a finally block or a with statement in a way that changes exception handling. continue in a finally block can suppress an active exception and alter the control flow unexpectedly. In general, avoid continue in finally unless you are certain about the consequences.

Performance and Maintainability Considerations

From a performance perspective, continue itself has negligible overhead. The interpreter simply jumps to the next iteration. The real benefit is avoiding unnecessary work: if a loop body performs expensive operations, using continue to skip irrelevant cases reduces the total number of operations executed.

for item in large_dataset: if not is_valid(item): continue expensive_processing(item)

This pattern prevents expensive_processing from being called on invalid items. The performance gain is proportional to how many items are skipped and how costly the skipped work would have been.

For maintainability, continue can make loops easier to read by flattening nested conditionals. However, overusing it can scatter the loop's logic across multiple skip points. A good rule is to use continue when the skipped case is a guard clause at the top of the loop, and to prefer a single if when the condition is simple and there is only one filter.

Limitations: Continue in Comprehensions and Exception Blocks

The continue statement cannot be used inside a list comprehension or any other comprehension expression. Python comprehensions have their own syntax for filtering, which is the if clause at the end.

# This is a syntax error # squares = [x * x for x in range(10) if x % 2 == 0 else continue] # Correct way to filter in a comprehension squares = [x * x for x in range(10) if x % 2 == 0]

Comprehensions are expression-oriented and do not support statements like continue. If you need more complex control flow, use a regular for loop.

Similarly, continue inside an except block is allowed, but it only affects the loop that encloses the try statement. The exception is still handled normally, and the loop moves to the next iteration after the except block completes. This can be useful for skipping items that cause exceptions, but it can also hide recurring errors. Use it deliberately and consider logging the exception before continuing.

for item in items: try: process(item) except ValueError as exc: log_error(exc) continue finalize(item)

Here, continue ensures that finalize is not called for items that raised a ValueError. The loop continues with the next item, and the error is recorded. This pattern is practical for batch processing where a single failure should not abort the entire run.

python continue statement: Practical Usage and Code Examples | RYUSLOG DEV