Beginner Chapter 67 · 20 min read · Module 5: Control Flow
continue Statement
- After this chapter you can explain what the continue statement does in a loop.
- You can use continue to skip unwanted items in a for loop.
- You can use continue safely in a while loop by updating the counter first.
- You can tell the difference between continue and break.
- You can rewrite a nested if block using continue as a guard.
Sometimes a loop should handle most items but ignore a few of them. You may want to print only the positive numbers in a list, or skip blank lines in a text. You could wrap the whole loop body in an if statement, but Python gives you a simpler tool called continue.
The continue statement skips the rest of the current round of the loop and jumps straight to the next round. The loop itself does not end. It simply moves on to the next item or checks its condition again. This makes it a perfect partner for filtering.
In this chapter you will use continue in for and while loops, compare it with break, and learn the one trap that catches many beginners in while loops: forgetting to update the counter before continue. You will also see how continue can make code flatter and easier to read.
What continue does
Inside a loop body, continue stops the current iteration at once. Python ignores the remaining lines of that round and starts the next one. In a for loop it takes the next item. In a while loop it goes back and tests the condition again.
Imagine a teacher handing out papers. If a student is absent, the teacher skips that desk and moves to the next one. The teacher does not go home; the round simply carries on. That is what continue does.
continue in a for loop
for n in range(1, 8):
if n % 2 == 1:
continue
print(n, "is even")When n is odd, continue skips the print line. Only even numbers reach it. Notice how the loop still visits every number from 1 to 7; it just does no work for the odd ones.
continue in a while loop
Be careful in a while loop. If continue runs before the line that changes the counter, the condition never changes and the loop repeats forever. Always update the counter before you use continue.
i = 0
while i < 6:
i += 1
if i == 3:
continue
print("i =", i)i += 1 is placed at the top, so even when continue runs, the counter has already moved forward.continue versus break
| Statement | Effect on the loop |
|---|---|
| continue | Skips the rest of this round, loop goes on |
| break | Ends the loop completely |
| pass | Does nothing, the rest of the round still runs |
for n in [1, 2, 3, 4]:
if n == 2:
continue
print("continue version:", n)
for n in [1, 2, 3, 4]:
if n == 2:
break
print("break version:", n)With continue the number 2 is skipped but 3 and 4 are still printed. With break the loop stops at 2, so nothing after it is seen.
A mental model for choosing
When you are unsure which statement to use, ask one question: should the loop keep going after this item? If the answer is yes, but this item needs no work, use continue. If the answer is no, because you have what you need or a problem makes further work pointless, use break. If the answer is yes and you only need an empty placeholder while you write the code later, use pass. Asking this question each time prevents most beginner mistakes with loop control.
It also helps to trace a loop on paper. Write down the value of the loop variable for each round, mark the rounds where continue runs, and cross out the lines that are skipped. After one or two such traces, the behaviour feels natural and you will predict outputs with confidence.
Using continue as a guard
A common style is to check for bad data first and continue right away. The good case then continues without extra indentation. This is often called a guard clause. It keeps the main work at the left edge of the loop, which is easier to read than several levels of nested if blocks.
words = ["apple", "", "kiwi", " ", "plum"]
for w in words:
if not w.strip():
continue
print(w.upper())Another benefit of guard clauses is that each rule is stated once, near the top, in plain words. A reader can see at a glance which items are ignored. If you later need a new rule, you add one more short check instead of rewriting a large nested block. Beginners often find this style friendlier, because every line of the loop body then has a single, clear job.
Blank and space only strings are skipped. Every real word is printed in capital letters. Use continue when it makes the intent clearer, but if the loop body is only one or two lines, a plain if may be simpler.
Python syntax
for item in sequence:
if condition:
continue
# work for the other itemswhile condition:
# update the counter first
if skip_condition:
continue
# normal workReal-world applications
- Skipping blank lines or comments when reading a text file.
- Ignoring invalid records while processing a list of data.
- Printing only items that match a rule, such as even numbers.
- Skipping players who are out of the game in a game loop.
- Cleaning data before calculating totals.
Common mistakes
Fix: Change the counter before the continue check, or at the top of the loop.
Fix: continue only skips the current round; the loop keeps running.
Fix: Only use continue inside a for or while body.
Fix: That code is skipped for that round; place it before continue if it must always run.
Important tips
- Update loop counters before continue in while loops.
- Use continue for early checks of bad data.
- Do not use too many continue lines in one loop.
- Print a debug message before continue to see what is skipped.
Key points summary
- continue skips to the next iteration.
- The loop does not end.
- In while loops the condition is checked again.
- It only works inside loops.
- It can replace deep nesting with guard checks.
Exam and interview points
- continue jumps to the next iteration of the nearest loop.
- In while loops the condition is re-evaluated after continue.
- continue does not prevent the loop else clause from running.
- continue outside a loop is a SyntaxError.
Quick revision
- continue = skip this round.
- break = stop the loop.
- Update counters first in while loops.
- Good for filtering and guard clauses.
Frequently asked questions
Does continue exit the loop?
No. It only skips the rest of the current iteration. The loop carries on with the next one.
Why did my while loop run forever after I added continue?
The counter update was probably after the continue line, so it was skipped. Move the update before continue.
Does continue affect the outer loop in nested loops?
No. It only applies to the innermost loop that contains it.
Can I use continue with the loop else clause?
Yes. continue does not stop the loop, so the else block still runs when the loop finishes normally.
Is continue required?
No. You can always use an if block instead. continue just makes some code flatter and cleaner.
Practise this chapter
16 practice questions Take the chapter quiz