Continue
Python · Loops
Theory
continue skips the rest of the current pass of a loop and jumps straight to the next one. In a for loop that means taking the next item; in a while loop it means going back to check the condition. In the example, every even n makes n % 2 == 0 true, so continue skips the print for that n, and the output is just the odd numbers 1 and 3. Unlike break, the loop keeps running until it naturally finishes.
continue is often used as a guard clause: check for data you want to ignore at the top of the body (a blank line, a missing value, a cancelled order) and continue, so the main logic below stays unindented and easy to read. In a while loop, be careful that the variable update still happens: if count += 1 sits after the continue, skipping it can create an infinite loop. continue only affects the innermost loop.
Why we use it
Real data contains rows you want to skip: blanks, headers, invalid values. continue lets you drop those cases early in the loop body without wrapping all the remaining code in an extra if block.
When to use it
Use continue to skip unwanted items while still processing the rest. Use break when you want to stop the loop completely. If you are only filtering to build a new list, a comprehension with an if is usually cleaner.
How it works
- for n in range(5): takes the next n
- Is n % 2 == 0 (even)? Yes: continue: skip the rest of this pass. No: print(n).
- Go to the next item
- Any items left? Yes: Next pass. No: Loop ends normally.
- Output: 1 and 3
Syntax
for item in iterable:
if condition:
continueExample
for n in range(5):
if n % 2 == 0:
continue
print(n)Key points
- Skips remaining code in the loop body for that iteration only
- Does not exit the loop, unlike break
- Great for guard clauses that skip bad or blank data
- In a while loop, make sure the counter update is not skipped
- Affects only the innermost loop