Python continue Statement
Table of Contents + β
In the last lesson you learned the break Statement. That one leaves a loop early and stops it for good. But sometimes you do not want to stop the whole loop. You just want to skip one item and keep going. That is exactly what continue is for.
π€ Why Do We Need continue?
Say you are looping over a list of names. A few of them are blank. You do not want to process the blank ones. But you also do not want to end the loop. The blank ones are not the end of your data. They are just gaps you want to ignore.
Without continue, you would wrap the rest of your loop body in an if. Then you indent everything deeper. The more things you want to skip, the messier that gets.
The continue statement fixes this. It says skip the rest of this turn and move on to the next one. And it stays inside the loop the whole time.
π§© What continue Actually Does
Here is the simple idea. A loop runs its body once for each item. When Python reaches continue, it stops the current turn right there. It does not run any lines below continue. Instead it jumps straight back to the top of the loop and grabs the next item.
Think of it like marking emails as read. You are going through your inbox one by one. You see a newsletter you do not care about. You do not delete the whole inbox, right? You just skip that email and look at the next one. continue is that skip.
The syntax is just one word on its own line. Usually it sits inside an if.
for item in items: if some_condition: continue # this line is skipped when the condition is true print(item)When some_condition is true, Python hits continue and goes back to the top for the next item. The print line never runs for that item.
π’ A Real Example: Skip the Even Numbers
Letβs print only the odd numbers from 1 to 10. We loop over every number. But when a number is even, we skip it.
for number in range(1, 11): if number % 2 == 0: continue print(number)Reading it line by line:
range(1, 11)gives us the numbers 1 through 10.number % 2 == 0is true when the number divides evenly by 2. That means it is even.- When that is true,
continueskips the rest of this turn. So theprintbelow never runs. - For odd numbers the
ifis false. So we skip pastcontinueand reach theprint.
So only the odd numbers make it to the screen.
Output
13579Notice we never wrote an else. The continue handles the βbadβ cases up top. That lets the rest of the loop body stay flat and easy to read.
π A More Real Example: Skip Blank Entries
Now something closer to real work. Imagine you collected sign-up names from a form. Some rows came in empty. You want to greet each real person and quietly skip the blanks.
names = ["Alex", "", "Arjun", " ", "Maya"]
for name in names: if name.strip() == "": continue print(f"Welcome, {name}!")Here is what happens:
name.strip()removes spaces from both ends of the text.- If what is left is an empty string, the entry is really blank. So we
continueand skip it. - A blank
""and a spaces-only" "both get skipped this way. - Real names fall through to the
printand get a welcome message.
Output
Welcome, Alex!Welcome, Arjun!Welcome, Maya!The two empty rows simply never reach the print. The loop did not stop. It just stepped over them.
π continue vs break
This is the part people mix up. So letβs make it clear. Both continue and break change how a loop runs. But they do very different things.
breakleaves the loop completely. Nothing after the loopβs current item runs. The loop is over.continueskips only the current turn. The loop keeps going with the next item.
Here they are side by side. First with break:
for number in range(1, 6): if number == 3: break print(number)Output
12The loop stops the moment it sees 3. We never get 4 or 5.
Now the same loop with continue:
for number in range(1, 6): if number == 3: continue print(number)Output
1245This time only 3 is missing. The loop skipped it and carried on to the end.
Here is the difference in one table:
| Statement | What it does | Does the loop continue? |
|---|---|---|
break | Leaves the loop right away | No, the loop ends |
continue | Skips the rest of this turn | Yes, with the next item |
β οΈ Common Mistakes
A couple of things confuse people with continue. Watch out for these.
Putting continue where there is no item left to skip can cause an endless loop in a while. If you skip before updating your counter, the counter never changes. Then the loop runs forever.
# β Avoid: count never changes when we skip, so this never endscount = 0while count < 5: if count == 2: continue # jumps back up with count still 2, forever print(count) count += 1In a while loop, update your counter before the continue. That way a skip still moves you forward.
# β
Good: count goes up first, so the skip is safecount = 0while count < 5: count += 1 if count == 3: continue print(count)Another mix-up is reaching for continue when a plain if would be clearer. If you only have one thing to do, just wrap it in an if. Save continue for when you want to skip several lines or several conditions cleanly.
# β Avoid: continue here is more confusing than a simple iffor n in range(1, 4): if n == 2: continue print(n)
# β
Good: a plain if reads better for a single actionfor n in range(1, 4): if n != 2: print(n)β Best Practices
- Use
continueto skip the cases you do not care about early. That keeps the main work in your loop flat and easy to read. - In a
whileloop, always make sure your counter changes before anycontinue. Otherwise you will loop forever. - Keep the
continuecondition simple. If theiftest gets long, pull it into a clearly named variable. - Reach for
continuewhen there are several lines to skip. For a single action, a plainifis often clearer.
π§© What Youβve Learned
β
continue skips the rest of the current loop turn and jumps to the next one.
β
Unlike break, continue does not leave the loop. It keeps it running.
β It is great for skipping items you do not care about, like even numbers or blank entries.
β
In a while loop, update your counter before continue so you never get stuck in an endless loop.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does the continue statement do?
Why: continue skips the remaining lines in the current turn and jumps to the next item, staying inside the loop.
- 2
What is the main difference between break and continue?
Why: break exits the loop entirely, while continue skips the current turn and keeps the loop running.
- 3
What does this print? for n in range(1, 6): if n == 3: continue print(n)
Why: Only 3 is skipped by continue; the loop keeps going, so it prints 1, 2, 4, and 5.
- 4
Why can continue cause an endless loop in a while loop?
Why: If continue jumps over the counter update, the counter never changes and the while condition stays true forever.
π Whatβs Next?
You now know how to skip a turn and how to leave a loop. Next up is a keyword that does nothing on purpose. It is more useful than it sounds.