What Is Continue Statement ?
The continue statement in Python is a loop control statement that skips the remaining code inside the current iteration of a loop and moves directly to the next iteration.When the loop encounters the continue keyword, it does not terminate the loop. Instead, it stops executing any lines of code written below it for that specific round and jumps straight to the top of the loop for the next cycle.
The continue Statement with a for Loop
In a for loop, when continue is triggered, the program immediately jumps to the next item in the sequence or collection.
Syntax
for item in sequence:
if condition:
continue # Skips the rest of the block for this item
# Code written here is skipped when 'continue' executes
Code Example
# Printing only odd numbers by skipping even numbers
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
continue # If the number is even, skip it instantly!
print(f"Processing Odd Number: {num}")
# Output:
# Processing Odd Number: 1
# Processing Odd Number: 3
# Processing Odd Number: 5
The continue Statement with a while Loop
Using continue inside a while loop requires extra caution. You must update your loop counter variable before calling continue. Otherwise, you will freeze your program in an infinite loop.
Syntax
while condition:
# ⚠️ Crucial: Increment/Update your variable before the 'continue' keyword
variable_update
if check_condition:
continue
# Code written here is skipped
Code Example
# Skipping a specific number while counting
i = 0
while i < 4:
i += 1 # Incrementing at the very top to avoid an infinite loop trap
if i == 2:
print("Found 2! Skipping it.")
continue
print(f"Current Count: {i}")
# Output:
# Current Count: 1
# Found 2! Skipping it.
# Current Count: 3
# Current Count: 4
Syntax of continue with if, elif, and else
When checking multiple conditions inside a loop using if-elif-else, you can use continue to selectively skip specific categories of data while processing others.
for item in sequence:
if condition_1:
continue # Skips item if condition_1 is True
elif condition_2:
# Code to process this specific condition
else:
# Code to process the remaining items
Comprehensive Code Example: continue with if-elif-else
Let's look at a realistic scenario. We have a list of numbers containing positive numbers, negative numbers, and zeroes.
1. If the number is negative, skip it completely using continue.
2. If the number is zero, print a warning message but do not process it further.
3. If the number is positive, square it and print the result.
numbers = [4, -2, 0, 7, -5, 3]
for num in numbers:
# Condition 1: Skip negative numbers entirely
if num < 0:
print(f"Skipping negative number: {num}")
continue # Instantly jumps to the next number in the list
# Condition 2: Handle zeroes specifically
elif num == 0:
print("Warning: Found a zero! Skipping calculation.")
continue # Skips the rest of the loop block for 0
# Condition 3: Process positive numbers
else:
squared_value = num ** 2
print(f"The square of positive number {num} is: {squared_value}")
print("Loop processing complete.")
Use Cases
Data Cleaning & Filtering : When analyzing data logs or data tables, you can use continue to skip empty cells, missing values (None), or corrupted lines while processing the rest of the dataset safely.
Access Control / Permitted Actions : Processing a list of website users and using continue to skip non-admin users, performing actions only on specified profiles without breaking the automation loop.