Python continue Statement
Table of Contents
In the last lesson, we learned about the Python break Statement. Now letβs skip one item while allowing the rest of the loop to continue.
π§© Letβs Talk About Skipping
Imagine we are going through a list of students. We want to send a message to everyone. But one student has already graduated. So we skip that one. And continue with the rest.
That is what continue does. It skips the rest of the current iteration. And moves to the next one.
π§ What Is continue in Python?
continue is a statement that skips the rest of the code in the current loop iteration. Then it goes back to the top of the loop for the next iteration.
It does not stop the loop. It just skips one round.
π Python continue in a for Loop
Letβs say we want to print numbers from 1 to 10. But skip the number 5.
for i in range(1, 11): if i == 5: continue print(i)Output
1234678910See what happened?
- The loop goes through 1 to 10.
- When
iis 5, the conditioni == 5is True. - So
continueruns. It skips theprint(i)line. - Then the loop moves to the next iteration.
ibecomes 6. - And it continues printing from 6 onwards.
So 5 is skipped. Everything else is printed.
π§ͺ Python continue in a while Loop
We can use continue in a while loop too.
count = 0
while count < 10: count += 1 if count % 2 == 0: continue print(count)Output
13579Here, we skip even numbers. When count is even, continue skips the print. So only odd numbers are printed.
Notice that count += 1 comes before the continue. That is important. If it came after, we would have an infinite loop. Because continue would skip the increment. And count would never change.
Letβs always make sure the loop variable is updated before continue.
βοΈ break vs continue in Python
These two are often confused. Let me clear it up.
breakstops the loop entirely. It exits.continueskips the current iteration. But the loop continues.
Think of it like this. We are watching a TV series.
breakis like turning off the TV. We stop watching.continueis like skipping one episode. We still watch the rest.
That is the difference.
for i in range(1, 6): if i == 3: break print(i)Output
12for i in range(1, 6): if i == 3: continue print(i)Output
1245See? break stopped at 3. continue just skipped 3 and kept going.
βοΈ Python continue: A Real-Life Example
Letβs say we have a list of numbers. We want to add only the positive numbers. Skip the negative ones.
numbers = [5, -3, 8, -1, 10, -7, 2]total = 0
for num in numbers: if num < 0: continue total += num
print(f"Sum of positive numbers: {total}")Output
Sum of positive numbers: 25Because 5 + 8 + 10 + 2 = 25. The negative numbers were skipped.
That is a clean way to filter things in a loop.
π Another Example β Skip a Specific Item
fruits = ["apple", "banana", "orange", "banana", "grape"]
for fruit in fruits: if fruit == "banana": continue print(fruit)Output
appleorangegrapeAll the bananas are skipped.
π§ continue in Nested Loops
Like break, continue only affects the innermost loop.
for i in range(1, 4): for j in range(1, 4): if j == 2: continue print(f"i={i}, j={j}")Output
i=1, j=1i=1, j=3i=2, j=1i=2, j=3i=3, j=1i=3, j=3See? When j is 2, it skips the print. But the inner loop continues to j=3. And the outer loop keeps going.
So continue only skips the current iteration of the inner loop.
β οΈ Common Mistakes with Python continue
β Mistake 1: Using continue Outside a Loop
if True: continue # Error!continue must be inside a loop.
π« Mistake 2: Skipping the Update in a while Loop
count = 0
while count < 5: if count == 3: continue print(count) count += 1This is an infinite loop. Because when count is 3, continue skips the count += 1. So count stays 3 forever.
So what do we do? We put the update before the continue.
count = 0
while count < 5: count += 1 if count == 3: continue print(count)Now it works.
π Mistake 3: Confusing continue with break
Letβs remember: continue skips one round. break stops the whole loop.
Next, we will learn about the Python pass Statement and use an empty placeholder where Python expects code.