Sub topic 6
Skipping an iteration with continue
This chapter assumes you can write a basic while loop, use if statements and comparison operators, and update a variable inside a loop body. You should also know that break leaves a loop entirely. Here you will learn the statement that does something different: continue ends only the current pass.
What continue does
continue stops the current iteration at once. Python skips the rest of the loop body and jumps back to the condition check. If the condition is still true, the loop runs again. If it is false, the loop ends normally.
Compare it with break:
| Statement | Effect on the current pass | What happens next |
|---|---|---|
| `break` | Stops immediately | The loop ends, and Python runs the code after it |
| `continue` | Stops immediately | Python re-checks the condition |
A working example
This loop prints only the odd numbers from 1 to 9.
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue
print(i)The output is 1, 3, 5, 7, 9. Follow one pass. When i becomes 2, the test i % 2 == 0 is true. continue runs, so print(i) is skipped. Python goes back to i < 10 and starts the next pass.
The line i += 1 comes before the continue. That placement matters, as the next section shows.
The classic bug: skipping the update
In a while loop, you are responsible for changing the state that the condition depends on. If continue jumps over that change, the condition never changes and the loop never ends.
i = 0
while i < 10:
if i % 2 == 0:
continue # bug: i is never incremented, so this loops forever
i += 1Here i starts at 0. The test is true, so continue runs and Python re-checks i < 10. Nothing has changed, so the same thing happens on every pass. The program hangs, and you must stop it with Ctrl+C.
This is a difference from a for loop. A for loop takes its next value from the iterable on its own, so continue is safe there. A while loop has no such automatic step.
Three ways to keep the update safe
- Update first. Change the state at the top of the body, as in the odd-number example. This works when you can use the new value for the rest of the pass.
- Update before each continue. Repeat the update inside the branch that skips. This is easy to forget when you add a second
continuelater. - Avoid continue. Put the main work inside an
ifand leave the update at the bottom, where it always runs.
Here is option 2 applied to the buggy loop:
i = 0
while i < 10:
if i % 2 == 0:
i += 1
continue
print(i)
i += 1It works, but the update now appears twice. Option 3 states the same logic more simply:
i = 0
while i < 10:
if i % 2 != 0:
print(i)
i += 1If a loop has several skip conditions, continue can still be clearer than deep nesting. Use it, and place the update first.
Using continue to filter input
continue works well as a guard that rejects unwanted items at the top of the body, so the rest of the body can assume the item is valid.
readings = ["12", "", "abc", "7", " ", "30"]
index = 0
total = 0
while index < len(readings):
text = readings[index].strip()
index += 1
if not text:
continue
if not text.isdigit():
print("Skipping bad reading:", text)
continue
total += int(text)
print("Total:", total)This prints Skipping bad reading: abc and then Total: 49. Empty strings are falsy, so not text catches blank entries. The index moves forward before either continue, so the loop always advances.
Common mistakes
- Putting the update after a continue. This causes the infinite loop shown above.
- Expecting continue to leave the loop. It only ends the current pass. Use
breakto leave. - Expecting continue to affect an outer loop. In nested loops,
continueapplies to the innermost loop only. - Using continue as the last statement of the body. It does nothing there, because the loop would continue anyway.
Exercise
This loop should print the numbers 1 to 5 except 3, but it never finishes. Why, and how do you fix it? n = 0 while n < 5: if n == 3: continue n += 1 print(n)
Show answer
When n is 3, continue runs before n += 1, so n stays 3 and the condition n < 5 stays true forever. (It is also off by one: it would print 1, 2, 3 before getting stuck, because n is printed after being incremented.) Fix it by moving the update to the top of the body: n = 0, then while n < 5: n += 1, then if n == 3: continue, then print(n). The loop now prints 1, 2, 4, 5 and ends.
Exercise
What is the difference between break and continue in a while loop?
Show answer
break ends the whole loop and runs the code after it. continue ends only the current iteration and sends Python back to the condition check, so the loop may run again.