Python continue Statement
continueis a keyword in Python used to skip the current loop iteration and proceed to the next iteration.
andbreakUnlike break,continueit does not terminate the entire loop; it only skips the remaining code of the current iteration.
Word Definition: continueIt means "continue"; it skips the current iteration and moves to the next.
Basic Syntax and Parameters
continueIt is a standalone statement and does not require any parameters.
Syntax Format
for item in iterable:
if 跳过条件:
continue
# 处理代码
Usage Scenarios
- Skip specific elements: Skip processing when conditions are met.
- Filter data: Exclude data that does not meet the conditions.
- Simplify logic: Move the "do not process" condition earlier to reduce nesting.
Examples
Example 1: Skipping Even Numbers
Examples
# Print the odd numbers from 1-10
for i in range(1, 11):
if i % 2 == 0:
continue # Skip even numbers
print(i, end=" ")
print()
# Output: 1 3 5 7 9
for i in range(1, 11):
if i % 2 == 0:
continue # Skip even numbers
print(i, end=" ")
print()
# Output: 1 3 5 7 9
Expected output:
1 3 5 7 9
Code analysis:
- When i is even, continue is executed, skipping the following print.
- When i is odd, print executes normally.
Example 2: Filtering List Elements
Examples
# Filter out empty values and None
data = ["apple", None, "", "banana", " ", "cherry"]
# Method 1: Using continue
result = []
for item in data:
if not item: # Empty string or None is False
continue
result.append(item)
print(result) # Output: ['apple', 'banana', ' ', 'cherry']
# Method 2: Only process valid ones
print("Valid data:")
for item in data:
if item: # Not empty and not None
print(f"- {item}")
data = ["apple", None, "", "banana", " ", "cherry"]
# Method 1: Using continue
result = []
for item in data:
if not item: # Empty string or None is False
continue
result.append(item)
print(result) # Output: ['apple', 'banana', ' ', 'cherry']
# Method 2: Only process valid ones
print("Valid data:")
for item in data:
if item: # Not empty and not None
print(f"- {item}")
Expected output:
['apple', 'banana', ' ', 'cherry'] 有效数据: - apple - banana - - cherry
continue can be used to filter data and exclude unwanted elements.
Example 3: Using continue in a while Loop
Examples
# Simulate skipping specific numbers
n = 0
while n < 10:
n += 1
if n == 5 or n == 7:
continue # Skip 5 and 7
print(n, end=" ")
print()
# Output: 1 2 3 4 6 8 9 10
n = 0
while n < 10:
n += 1
if n == 5 or n == 7:
continue # Skip 5 and 7
print(n, end=" ")
print()
# Output: 1 2 3 4 6 8 9 10
Expected output:
1 2 3 4 6 8 9 10
continue has the same effect in while loops.
Example 4: Simplifying Code Logic
Examples
# Use continue to simplify logic
numbers = [1, 2, -3, 4, -5, 6, -7]
# Without using continue (deeper nesting)
print("Positive numbers (Method 1):")
for n in numbers:
if n > 0:
print(n)
print("\nPositive numbers (Method 2) - using continue):")
for n in numbers:
if n <= 0:
continue
print(n)
print("\n"Negative numbers:")
for n in numbers:
if n >= 0:
continue
print(n)
numbers = [1, 2, -3, 4, -5, 6, -7]
# Without using continue (deeper nesting)
print("Positive numbers (Method 1):")
for n in numbers:
if n > 0:
print(n)
print("\nPositive numbers (Method 2) - using continue):")
for n in numbers:
if n <= 0:
continue
print(n)
print("\n"Negative numbers:")
for n in numbers:
if n >= 0:
continue
print(n)
Expected output:
Using continue can make the code flatter and reduce nesting.
Example 5: continue in Nested Loops
Examples
# continue only affects the current loop
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
continue # Skip the current iteration of the inner loop
print(f"({i},{j})", end=" ")
print()
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
continue # Skip the current iteration of the inner loop
print(f"({i},{j})", end=" ")
print()
Expected output:
(1,1) (1,3) (2,1) (2,3) (3,1) (3,3)
continue only skips the current iteration of the innermost loop that contains it.
Other Extensions
Python3 Loop Statements