Python continue Statement

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

1
2
3
4
6
7
8
9
10

See what happened?

  • The loop goes through 1 to 10.
  • When i is 5, the condition i == 5 is True.
  • So continue runs. It skips the print(i) line.
  • Then the loop moves to the next iteration. i becomes 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

1
3
5
7
9

Here, 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.

  • break stops the loop entirely. It exits.
  • continue skips the current iteration. But the loop continues.

Think of it like this. We are watching a TV series.

  • break is like turning off the TV. We stop watching.
  • continue is 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

1
2
for i in range(1, 6):
if i == 3:
continue
print(i)

Output

1
2
4
5

See? 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: 25

Because 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

apple
orange
grape

All 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=1
i=1, j=3
i=2, j=1
i=2, j=3
i=3, j=1
i=3, j=3

See? 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 += 1

This 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.