The continue Statement
Overview
The continue statement in Python is used to skip the remaining code inside a loop and proceed directly to the next iteration of the loop. It is often used when you want to ignore certain iterations based on a condition.
Syntax
The continue statement is placed inside a loop, whether it’s a for loop or a while loop. Here’s the basic syntax:
while condition: if condition_to_continue: continue # Other statements
or
for item in sequence: if condition_to_continue: continue # Other statements
Example with while
i = 0 while i < 5: if i % 2 == 0: i += 1 continue print(i) i += 1 # Output: # 1 # 3
Explanation:
- The while loop continues as long as i is less than 5.
- If i is even (i % 2 == 0), the continue statement is executed. This causes the loop to skip the rest of the code (in this case, print(i)) for that iteration and move on to the next iteration.
- Even numbers are not printed, only odd numbers are displayed.
Example with for
fruits = ["apple", "banana", "cherry", "date", "fig"] for fruit in fruits: if fruit.startswith('d'): continue print(fruit) # Output: # apple # banana # cherry # fig
Explanation:
- The for loop iterates over the list fruits.
- If the fruit starts with the letter ‘d’, the continue statement is executed, which skips the remaining code (in this case, print(fruit)) for that iteration.
- Fruits starting with ‘d’ are not printed.
Common Use Cases
- Skipping Specific Iterations:
- Use continue to skip certain iterations of a loop based on specific conditions.
Example:
for number in range(10): if number % 2 == 0: continue print(number) # Output: # 1 # 3 # 5 # 7 # 9
- Filtering Data:
- Use continue to filter out irrelevant data while processing information.
Example:
data = [5, 10, 15, 20, 25] for value in data: if value % 2 != 0: continue print(value) # Output: # 10 # 20
- Optimizing Complex Loops:
- In complex loops with multiple conditions, continue can help avoid excessive indentation by managing specific cases more effectively.
Example:
for i in range(10): if i == 3 or i == 7: continue print(i) # Output: # 0 # 1 # 2 # 4 # 5 # 6 # 8 # 9
Behavior with Nested Loops
In nested loops, the continue statement affects only the loop in which it is placed. The outer loops continue to execute as usual.
Example:
for i in range(3): for j in range(3): if j == 1: continue print(f"i={i}, j={j}") # Output: # i=0, j=0 # i=0, j=2 # i=1, j=0 # i=1, j=2 # i=2, j=0 # i=2, j=2
Explanation:
- The inner loop continues with the next iteration when j equals 1 due to the continue statement.
- The outer loop continues normally.
Best Practices
- Code Clarity:
- Use continue to improve code readability by avoiding excessive levels of indentation.
- Comment its usage if the condition is not immediately clear.
- Performance:
- Ensure that using continue does not make the loop less performant or overly complex.
- Alternatives:
- In some cases, it might be preferable to restructure loop conditions rather than using continue.
Summary
The continue statement is a useful tool for controlling the flow of loops in Python. It allows you to skip directly to the next iteration of the loop, which can simplify data processing and improve code readability by avoiding deep nesting. When used appropriately, continue can make your code cleaner and more efficient.