Python continue Statement: Skip the Current Loop Iteration
Learn how Python's continue statement skips the rest of the current for or while loop iteration and proceeds with the next one.
What does continue do?
continue is a loop-control statement. It stops the current loop iteration early and starts the next iteration.
A loop iteration is one pass through a loop body. The loop body is the indented block of statements that runs repeatedly. When Python reaches continue, statements below it in the same loop body do not run.
The loop itself does not end. In a for loop, Python obtains the next item from the iterable. In a while loop, Python checks the loop condition again after control returns to the top.
for item in iterable:
if condition_to_skip:
continue
process(item)The condition determines whether the current item is skipped. If the condition is false, execution continues normally and reaches process(item).
Where can you use continue?
You can use continue inside both for loops and while loops. It is commonly placed inside an if conditional statement when a particular case should be ignored.
for number in numbers:
if number < 0:
continue
print(number)This example rejects negative numbers. For a negative value, continue skips the print statement. Valid values reach the rest of the loop body.
continue must be inside an enclosing for or while loop. Using it outside a loop produces a syntax error because there is no current iteration to continue.
if ready:
continue # SyntaxError: not inside a loopExecution flow
- Python begins an iteration and assigns or evaluates the current loop value.
- The loop body runs from top to bottom.
- An
ifcondition tests whether this iteration should be skipped. - If the condition is true,
continuestops the current iteration immediately. - Statements below
continueare not executed for that iteration. - The loop obtains or evaluates its next iteration value and repeats.
If the condition is false, Python does not execute continue. It runs the remaining statements in the loop body normally.
Example: print numbers except 5
This for loop uses range(1, 11). The starting value is 1, and the ending value 11 is excluded, so the loop visits 1 through 10.
for number in range(1, 11):
if number == 5:
continue
print(number)Output:
1
2
3
4
6
7
8
9
10When number is 5, the condition is true. Python reaches continue, skips print(number), and then obtains the next value, 6. That is why 5 is absent while all later values are still processed.
Trace of the loop
Example: omit vowels from text
Strings are iterable, so a for loop can process one character at a time. The following program reads a word or phrase and skips vowels.
text = input("Enter a word or phrase: ")
for character in text:
if character.lower() in "aeiou":
continue
print(character, end="")If the user enters Hello, world!, the output is:
Hll, wrld!character.lower() makes uppercase vowels match the lowercase vowel set. For a vowel, continue skips the print statement. Consonants continue through the rest of the loop body and are printed.
Spaces, punctuation, digits, and other non-vowel characters are also printed because they do not satisfy the vowel condition.
For more practice with character iteration, see Python strings and accessing individual characters.
Filtering invalid values
A practical use of continue is rejecting unwanted data before the main processing logic runs.
values = [12, -3, 8, 0, -1, 15]
for value in values:
if value < 0:
continue
print("Processing", value)Only 12, 8, 0, and 15 are processed. Negative values are ignored without ending the loop.
continue compared with break and pass
break exits the nearest enclosing loop entirely. Unlike continue, it prevents all later iterations of that loop. pass does nothing; it does not change the flow of the loop.
Side-by-side behavior
for number in range(1, 5):
if number == 2:
continue
print("continue example", number)
for number in range(1, 5):
if number == 2:
break
print("break example", number)
for number in range(1, 5):
if number == 2:
pass
print("pass example", number)The first loop prints 1, 3, and 4. The second prints only 1 because break ends the loop at 2. The third prints 1, 2, 3, and 4 because pass allows execution to reach the later print.
Use continue when one iteration should be skipped, break when the loop should end, and pass when Python requires a statement but no action is currently needed. For more detail, see the Python break statement and the Python pass statement.
Be careful in while loops
A while loop repeats while its condition remains true. Its counter or other loop-control state must still be updated on every path that can reach the next condition check.
This version can become an infinite loop:
number = 1
while number <= 5:
if number == 3:
continue
print(number)
number += 1When number becomes 3, continue skips the increment. The value remains 3, so the condition remains true forever.
Update the loop state before the possible continue:
number = 1
while number <= 5:
current = number
number += 1
if current == 3:
continue
print(current)Output:
1
2
4
5The counter advances on every iteration, including the skipped one, so the loop terminates normally.
continue or an inverse condition?
Use continue when the rejection rule is clearer when stated directly:
for value in values:
if value < 0:
continue
process(value)For a simple case, an inverse condition may be equally readable:
for value in values:
if value >= 0:
process(value)Choose the version that makes the main action easiest to understand. Keep conditions clear and avoid unnecessary nesting. A long series of skip conditions can sometimes be better expressed with a separate filtering step or a comprehension.
Troubleshooting
Code after continue does not run
This is expected behavior. continue immediately ends the current iteration. Move essential work above it, or restructure the condition so that only optional work is skipped.
The while loop runs forever
The counter or other loop-control state is probably updated after continue. Move the update before the conditional skip, as shown in the corrected example.
The loop stops instead of skipping one item
Check whether you used break. Replace it with continue when later iterations must still run.
A syntax error occurs
Make sure continue is indented inside a for or while loop. It is invalid outside a loop.
Uppercase vowels are not skipped
Normalize the character with lower(), or test both uppercase and lowercase vowels:
if character.lower() in "aeiou":
continueKey points
continueskips all remaining statements in the current loop iteration.- It then proceeds to the next iteration; it does not end the loop.
- It is valid inside
forandwhileloops, usually within anifstatement. breakexits the nearest loop, whilepassdoes nothing.- In a
whileloop, update the loop-control state before a possiblecontinue. - Use it when explicitly rejecting an item makes the loop easier to read.