continue statement
continue skips the rest of the current loop round and jumps straight to the next one, without ending the loop.
for item in iterable:
if condition:
continue
# skipped for items that matchedcontinue tells the loop: "I am done with this item, go to the next one". The lines below it in the loop body are skipped for this round only.
It keeps loops flat and readable when you want to ignore some items, such as blank lines in a file or invalid entries, instead of wrapping the whole body in an if.
In a while loop, be careful: if the code that changes the condition comes after continue, it gets skipped too, and the loop may never end.
Examples
1Skip some items
for n in range(1, 8):
if n % 2 == 0:
continue
print(n)1 3 5 7
Even numbers reach continue, so print is skipped for them.
2Ignore blank lines and comments
lines = ["milk", "", "# weekly list", "eggs", " ", "rice"]
for line in lines:
line = line.strip()
if not line or line.startswith("#"):
continue
print("Buy:", line)Buy: milk Buy: eggs Buy: rice
3Skip bad values while adding up
entries = ["12", "abc", "7", "", "5"]
total = 0
for e in entries:
if not e.isdigit():
print("Skipping", repr(e))
continue
total += int(e)
print("Total:", total)Skipping 'abc' Skipping '' Total: 24
4continue in a while loop
The counter goes up before continue, so the loop still moves forward.
n = 0
while n < 6:
n += 1
if n == 3:
continue
print(n)1 2 4 5 6
Common mistakes
Skipping the counter update in a while loop
If n += 1 comes after continue, the round that hits continue never updates n, and the loop runs forever. Update the counter first, as in the last example, or use a for loop.
Using continue where if/else is clearer
For a short body, a plain if reads better than continue. Use continue to skip early when the rest of the body is long.
Questions people ask
continue vs break vs pass?
continue jumps to the next round, break statement ends the loop, and pass statement does nothing at all: it is just a placeholder.
Does continue skip the else block of a loop?
No. Only break skips the loop's else. A loop that uses continue still runs else when it finishes.
Can I use continue outside a loop?
No, it is a SyntaxError. It only works inside for and while.