continue

Updated March 15, 2026 · Keywords
keyword control-flow loop

The continue statement in Python skips the rest of the current loop iteration and moves directly to the next iteration. Unlike break, which exits the loop entirely, continue only jumps ahead within the current iteration.

Syntax

while condition:
    # loop body
    if skip_condition:
        continue  # skip to next iteration
    # remaining code skipped when continue is triggered

for item in iterable:
    # loop body
    if skip_condition:
        continue  # skip to next iteration
    # remaining code skipped when continue is triggered

How It Works

When Python encounters a continue statement inside a loop:

  1. The rest of the current iteration is skipped
  2. The loop condition is re-evaluated (for while) or the next item is fetched (for for)
  3. Execution continues with the next iteration
# Skip even numbers
for i in range(1, 6):
    if i % 2 == 0:
        continue
    print(i)
# Output: 1, 3, 5

Common Use Cases

Filtering items during iteration

# Process only positive numbers
numbers = [1, -2, 3, -4, 5, -6]

for num in numbers:
    if num <= 0:
        continue
    print(f"Processing {num}")
# Output: Processing 1, Processing 3, Processing 5

Skipping invalid data

# Skip empty or whitespace-only strings
data = ["hello", "", "world", "   ", "python"]

for item in data:
    if not item.strip():
        continue
    print(f"Valid: {item}")
# Output: Valid: hello, Valid: world, Valid: python

Conditional processing

# Only process files with specific extension
files = ["doc.txt", "image.jpg", "data.csv", "script.py", "readme.md"]

for file in files:
    if not file.endswith((".txt", ".csv")):
        continue
    print(f"Processing {file}")
# Output: Processing doc.txt, Processing data.csv

Skip certain iterations in while loop

# Skip iteration when a condition is met
count = 0
while count < 10:
    count += 1
    if count % 3 == 0:
        continue
    print(count)
# Output: 1, 2, 4, 5, 7, 8, 10

Using with else

Like break, continue also affects the else clause of a for loop. The else block runs when the loop completes normally (without break), regardless of continue statements:

# Check all items meet a condition
items = [2, 4, 6, 8, 9]

for item in items:
    if item % 2 != 0:
        print(f"Found odd number: {item}")
        break
else:
    print("All items are even!")
# Output: Found odd number: 9

continue vs break

Understanding when to use continue versus break:

# break: exit the loop entirely
for item in items:
    if item == target:
        print(f"Found {item}")
        break  # stop looking
# Output after finding target

# continue: skip current iteration, keep looking
for item in items:
    if item == target:
        continue  # skip this one, check next
    print(item)
# Output all items except target

Practical comparison

# Using break to find and stop
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
for fruit in fruits:
    if len(fruit) > 5:
        print(f"Found long name: {fruit}")
        break
# Output: Found long name: cherry

# Using continue to filter
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
for fruit in fruits:
    if len(fruit) <= 5:
        continue
    print(f"Long name: {fruit}")
# Output: Long name: cherry, Long name: elderberry

Practical Examples

Data validation

# Skip invalid records during processing
records = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": -1},
    {"name": "Charlie", "age": 30},
    {"name": "Diana", "age": 0},
]

valid_records = []
for record in records:
    if record["age"] < 0:
        print(f"Skipping invalid age for {record[name]}")
        continue
    valid_records.append(record)

print(valid_records)
# Output: [{name: Alice, age: 25}, {name: Charlie, age: 30}]

Skip header row

# Process CSV data, skip header
rows = [
    ["Name", "Age"],  # header
    ["Alice", "25"],
    ["Bob", "30"],
    ["Charlie", "35"],
]

for i, row in enumerate(rows):
    if i == 0:
        continue  # skip header
    print(f"Processing: {row[0]}, {row[1]}")
# Output: Processing: Alice, 25, Processing: Bob, 30, Processing: Charlie, 35

Nested loops with continue

continue applies to the innermost loop when used in nested loops:

# Skip even numbers in inner loop
for i in range(3):
    for j in range(3):
        if j % 2 == 0:
            continue
        print(f"i={i}, j={j}")
# Output:
# i=0, j=1
# i=1, j=1
# i=2, j=1

Alternative patterns

Sometimes a different approach is clearer than continue:

# Using continue
for item in items:
    if not is_valid(item):
        continue
    process(item)

# Alternative: filter first
valid_items = [item for item in items if is_valid(item)]
for item in valid_items:
    process(item)

# Alternative: early return in a function
def process_items(items):
    for item in items:
        if not is_valid(item):
            return  # or continue if in a loop
        process(item)

See Also

  • break — exit a loop entirely
  • range — generate sequences for looping
  • enumerate — get index and value during iteration