In Python, continue skips the rest of the current loop iteration and jumps to the next one. It works inside for and while loops, and the loop keeps running until its normal end.
The usual shape is a guard at the top of the loop body: test for an item you want to ignore, call continue, and write the real work below without extra indentation.
for line in ["alpha", "", "beta", " ", "gamma"]:
if not line.strip():
continue
print(line.upper())
Example output:
ALPHA
BETA
GAMMA
What continue does, exactly
The Python language reference defines it in one sentence: continue "continues with the next cycle of the nearest enclosing loop." The syntax is just the keyword, with no arguments. When it runs, everything after it in the loop body is skipped for that pass. In a for loop, Python then fetches the next item from the iterable. In a while loop, Python goes back and re-evaluates the condition.
The reference also sets two placement rules. continue may only appear syntactically nested inside a for or while loop, and it may not sit inside a function or class definition that is itself inside the loop. Python checks this when it compiles the file, so a misplaced continue stops the whole script before any line runs, not just when execution reaches it.
The official example: even and odd numbers
The Python tutorial shows continue with this loop. Even numbers print a message and skip the final line. Odd numbers fall through to it:
for num in range(2, 10):
if num % 2 == 0:
print(f"Found an even number {num}")
continue
print(f"Found an odd number {num}")
Output, as given in the tutorial:
Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5
Found an even number 6
Found an odd number 7
Found an even number 8
Found an odd number 9
The same loop can be written with else instead of continue. The continue version is flatter, and the difference grows as the amount of work after the guard grows. For more patterns that combine loops and conditions, see Python for loop with if condition.
Continue in a while loop
In a while loop, continue sends control back to the condition check. This version prints only the odd numbers from 1 to 5:
n = 0
while n < 6:
n += 1
if n % 2 == 0:
continue
print(n)
Example output:
1
3
5
The increment n += 1 comes before the continue. That order matters, as the next section shows.
The infinite-loop trap
A for loop moves forward on its own, so continue can never stall it. A while loop only moves forward if your code changes the variable in the condition. If continue jumps over that change, the condition never changes and the loop never ends:
n = 0
while n < 6:
if n % 2 == 0:
continue # n is 0, so this runs forever
n += 1
print(n)
Here n starts at 0, the test is true, continue returns to the condition, and n is still 0. The program hangs with no output. Press Ctrl+C to stop it, then update the loop variable before the continue, as in the previous example, or restructure the loop so the update happens at the top.
Continue in nested loops
continue affects only the innermost loop it sits in. The outer loop carries on as normal:
for row in [[1, -2, 3], [-4, 5]]:
for value in row:
if value < 0:
continue
print(value)
Example output:
1
3
5
Each negative value is skipped by the inner loop, and both rows are still visited. Python has no labelled continue. If you need to skip to the next outer iteration from inside the inner loop, either set a flag and check it after the inner loop, or move the inner loop into a small function that returns a result, and call continue in the outer loop based on that result.
Continue with try and finally
If continue leaves a try block that has a finally clause, the finally code runs first. The language reference states that the clause "is executed before really starting the next loop cycle". This makes continue safe to use when the loop holds something that needs cleanup:
for name in ["a", "b"]:
try:
if name == "a":
continue
print("processing", name)
finally:
print("cleanup", name)
Example output:
cleanup a
processing b
cleanup b
For "a", the continue skips the print("processing", ...) line, but the cleanup line still runs. For handling errors inside a loop and moving on to the next item, try and except with continue in the except block is a common pattern. See Python exception handling with try and except for the exception side of that.
Continue vs break vs pass
These three are often confused. They differ in what happens to the rest of the loop body and to the loop itself:
| Statement | Rest of this iteration | Loop | Loop else clause |
|---|---|---|---|
continue | Skipped | Goes on with the next iteration | Still runs at the end if no break happened |
break | Skipped | Ends immediately | Does not run |
pass | Still runs | Goes on normally | Still runs at the end if no break happened |
The tutorial describes the loop else rule this way: the clause runs when the loop finishes without executing break, and it does not run if break ended the loop. Since continue never ends the loop, it has no effect on that rule.
Skipping items without continue
continue is not the only way to skip items, and sometimes it is not the clearest. Two alternatives:
- Invert the condition. If the guard is short and the work is one or two lines,
if value >= 0: print(value)reads just as well and needs nocontinue. - Filter first. A comprehension or generator expression with a trailing
ifdrops unwanted items before the loop starts:for value in (v for v in row if v >= 0):. A comprehension cannot containcontinueitself, because it is a statement and only allowed in a loop body.
Reach for continue when you have several different reasons to skip an item, for example a blank line, a comment line and a malformed line. A chain of early continue guards keeps the real work at one indentation level instead of nesting it three if blocks deep.
If it still behaves unexpectedly
- The loop hangs. You are probably in a
whileloop wherecontinueskips the line that updates the loop variable. Move the update above thecontinue. - Nothing after the guard runs. Check the guard condition.
continueskips everything below it for every item where the condition is true, so a condition that is always true skips the whole body. - The script won't start and reports a syntax error. The
continueis outside a loop, or inside a function defined within the loop. Move it into a loop body of its own. - The wrong loop is skipped. In nested loops,
continuebinds to the innermost loop. Use a flag or a helper function for the outer one.
Frequently asked questions
- What does continue do in Python?
- The continue statement ends the current iteration of the nearest enclosing for or while loop and starts the next one. Any code below it in the loop body is skipped for that iteration only. The loop itself keeps running.
- What is the difference between continue and break in Python?
- continue skips to the next iteration and the loop carries on. break leaves the loop entirely, and a loop's else clause is skipped when break runs. Use continue to ignore one item, and break when you are finished with all of them.
- What is the difference between continue and pass in Python?
- pass does nothing and the rest of the loop body still runs. continue jumps straight to the next iteration and skips whatever comes after it. pass is a placeholder for an empty block, continue is flow control.
- Can I use continue in a Python list comprehension?
- No. continue is a statement and may only appear inside a for or while loop body. In a comprehension, put the condition at the end instead, for example [x for x in items if x > 0], which skips the items that fail the test.
- Does continue in an inner loop affect the outer loop?
- No. continue applies only to the nearest enclosing loop. The outer loop is not skipped. To skip an outer iteration from inside an inner loop, use a flag variable, or move the inner loop into a function and return a result.