A while loop may need to skip one value and still check its condition again for the next iteration. The Python continue statement skips the rest of the current iteration and moves to the next one.

I’ll show how to place continue in a while loop and explain why a condition variable may need updating before it runs.

TL;DR

The Python continue statement skips the rest of the current loop iteration and moves to the next one.

  • Use it inside a for or while loop, often under an if condition.
  • In nested loops, it affects the nearest enclosing loop only.
  • In a while loop, update the condition variable before continue when needed.

What Is the Python continue Statement?

continue is a loop-control statement that ends the current pass through a loop body early. Python skips the statements after it in that iteration, then starts the next iteration.

The statement has no condition of its own, so an if statement usually decides when to reach it. Its full syntax is just the keyword:

I ran the loop examples below with Python 3.11.16. In each one, the print after continue is skipped only for the matching value.

How to Use continue in Python Loops

Put continue under the condition that identifies an iteration to skip. The following examples show the placement in both loop forms.

Step 1: Skip a value in a for loop

This for loop prints each value except 3. The print statement after continue does not run for that value.

for item in (1, 2, 3, 4):
    if item == 3:
        continue
    print(item)
print("done")

Output:

The loop continues after 3 and reaches 4. The final print is outside the loop, so it runs after iteration ends.

Step 2: Skip a value in a while loop

This while loop increments count before testing the condition for the skipped value. That keeps the loop moving when continue bypasses the later increment.

count = 0
while count < 5:
    count += 1
    if count == 3:
        continue
    print(count)

Output:

Terminal output of python3 while_continue_demo.py: 1, 2, 4, 5, with 3 skipped by continue

Here the while condition is checked again after continue. The increment happens before the condition, so count advances past 3 rather than repeating it forever.

Common continue errors and loop scope

continue must appear within a for or while loop. An if statement can contain it, but the if statement does not make it valid outside a loop. When it is outside loop scope, Python reports a SyntaxError.

In nested loops, continue applies to the nearest enclosing loop, not every loop around it. This example skips 3 in the inner loop and then proceeds to 4.

for row in ((1, 2), (3, 4)):
    for value in row:
        if value == 3:
            continue
        print(value)

Output:

Terminal output of python3 nested_continue_demo.py: 1, 2, 4, with continue skipping 3 in the inner loop

Use break when the loop should stop, and pass when a statement is required but no action should happen. That changes the control flow in different ways:

Statement Effect
continue Skips the rest of this iteration and advances the loop.
break Exits the nearest enclosing loop.
pass Does nothing and leaves normal control flow unchanged.

Example: Checking Prime Factors

This example uses continue to skip even candidates above 2 before checking the remaining numbers for divisors. It prints the primes from 2 through 10.

for number in range(2, 11):
    if number > 2 and number % 2 == 0:
        continue
    for candidate in range(2, number):
        if number % candidate == 0:
            break
    else:
        print(number)

Output:

Terminal output of python3 prime_continue_demo.py: the primes 2, 3, 5 and 7

The output omits 4, 6, 8, and 10 because each is divisible by 2. The condition keeps 2, which is prime, in the search. The inner for-else prints a number only when no divisor triggered break.

The two statements handle different decisions. continue skips an even candidate and advances the outer loop. break ends the inner divisor search when it finds a factor.

The Python tutorial documents this loop-else behavior for prime-number searches. Use continue when later work in the current iteration should be skipped but the loop still has useful iterations to process.

When Should You Use continue?

Use continue when one iteration has no useful remaining work, but the loop should still process later values. Put the condition before the statements you want to skip, and keep the statements that must run for every item before that condition.

A sentinel such as None can mark an input that should not be formatted as a value. This loop skips that entry and processes the others:

for value in (2, None, 5):
    if value is None:
        continue
    print(value * 2)

Output:

Terminal output of python3 skip_none_continue_demo.py: 4 and 10, with None skipped

The loop skips None before multiplication, but continues with later values. If you need to stop at the first matching item instead, use break.

A continue can also make control flow harder to follow when several conditions send execution to different places. In that case, an if/else structure may make the work for each case clearer. The useful test is whether skipping the remainder of one pass reads more clearly than nesting the remaining statements under the opposite condition.

The loop type determines what happens after continue:

Loop After continue
for Python requests the next item from the iterator.
while Python checks the condition again, so update any state it depends on before continuing.

Conclusion: Keep continue Inside the Loop You Mean to Skip

Place continue where the skipped work begins, and check that a while loop still updates whatever its condition depends on. For loop behavior and syntax details, see the Python for loop, Python while loop, and Python Language Reference.

Frequently Asked Questions

These answers clarify loop scope and show how continue differs from other loop-control statements.

Can continue be used inside an if statement?

Yes, but only when the if statement is inside a for or while loop. The if condition chooses when to skip the current pass, but it does not create loop scope. A continue outside a loop is invalid and raises SyntaxError.

Does continue affect every enclosing loop?

No. In nested loops, Python applies continue to the nearest enclosing loop, so the inner loop advances and the outer loop resumes its body when the inner loop finishes. The language reference specifies that continue targets the nearest enclosing for or while loop.

What is the difference between continue, break, and pass?

continue skips the rest of the iteration and advances to the next one, while break exits the nearest loop. pass does nothing, so execution moves to the next statement in the same iteration. Choose the statement that matches the control-flow change you need.

Can I continue to the next outer loop iteration?

A plain continue always targets the nearest enclosing loop. Python’s syntax does not provide a label for an outer loop, so restructure nested control flow or use a condition or helper function to make that path explicit. PEP 3136 records the proposal for labeled break and continue and its rejection.

Why can continue make a while loop run forever?

If continue skips the only statement that changes the while condition, the next condition check can see the same value again. Put the required update before the continue path, as in the earlier count example, or ensure every path changes the state used by the condition.

Can you use continue in an if statement in Python?

Yes, when that if statement is inside a for or while loop. continue outside a loop raises SyntaxError.

Does continue skip the whole loop?

No. It skips the remainder of the current iteration and proceeds to the next iteration of the nearest enclosing loop.

What is the difference between continue and pass?

continue advances a loop to its next iteration. pass does nothing and does not change the flow of execution.

Share.
Leave A Reply