The continue statement in Python is used inside loops to skip the current iteration and move directly to the next one. It does not stop the loop completely (like break), it only skips a single cycle.
This is very useful when you want to ignore certain values or conditions while still continuing the loop.
🔹 What is continue in Python?
The continue statement tells Python:
“Skip everything below me in this loop and go to the next iteration.”
It works in both:
-
forloops -
whileloops
🔹 Syntax of Continue Statement
continue
It is usually used with an if condition inside a loop.
🔹 Flow of Continue Statement
When Python executes continue:
- It stops the current loop iteration
- Skips remaining code in that loop block
- Moves to next iteration
🔹 Example 1: Using continue in for loop
for i in range(1, 6):
if i == 3:
continue
print(i)
🔸 Output:
1
2
4
5
🔍 Explanation:
-
When
i == 3, the loop skipsprint(i) -
So
3is not printed
🔹 Example 2: Skip even numbers
for num in range(1, 11):
if num % 2 == 0:
continue
print(num)
🔸 Output:
1
3
5
7
9
🔍 Explanation:
-
Even numbers are skipped using
continue - Only odd numbers are printed
🔹 Example 3: Using continue in while loop
i = 0
while i < 5:
i += 1
if i == 2:
continue
print(i)
🔸 Output:
1
3
4
5
🔹 Real-Life Use Case of Continue
Imagine you are processing a list of users, but you want to skip inactive accounts.
users = ["admin", "guest", "inactive", "user1"]
for user in users:
if user == "inactive":
continue
print("Processing:", user)
🔸 Output:
Processing: admin
Processing: guest
Processing: user1
🔹 Difference Between break and continue
| Feature | break | continue |
|---|---|---|
| Action | Stops loop completely | Skips current iteration |
| Loop continues? | ❌ No | ✅ Yes |
| Usage | Exit loop early | Skip specific case |
🔹 Common Mistakes
❌ Forgetting condition:
for i in range(5):
continue
print(i) # never runs
❌ Wrong placement of continue:
-
It should be inside a loop and usually inside
if
🔹 Key Points to Remember
-
continueonly affects current iteration - Loop does NOT stop completely
-
Works in both
forandwhileloops - Very useful for filtering data
🚀 Conclusion
The Python continue statement is a powerful tool for controlling loop behavior. It helps you skip unwanted values and keep your code clean and efficient.
If you are working with data filtering, validation, or iteration logic, continue is one of the most useful loop controls you will use.


0 Comments