continueIntermediate Examples

Skips the rest of the current loop iteration and moves to the next

Continue in data processing

Filtering and transforming data with continue.

python
# Process only valid records
records = [
    {"name": "Alice", "age": 30},
    {"name": "", "age": 25},
    {"name": "Bob", "age": -1},
    {"name": "Charlie", "age": 35},
]

valid = []
for record in records:
    if not record["name"]:
        continue
    if record["age"] < 0:
        continue
    valid.append(record)

print(f"Valid records: {len(valid)}")
for r in valid:
    print(f"  {r['name']}, age {r['age']}")
Expected Output
Valid records: 2
  Alice, age 30
  Charlie, age 35

continue is useful for guard clauses at the top of a loop body, keeping the main logic un-indented and readable.

Continue in while loops

Using continue with while-based iteration.

python
# Retry pattern with continue
import random
random.seed(42)

attempts = 0
successes = 0
while successes < 3:
    attempts += 1
    if attempts > 20:
        break
    value = random.randint(1, 6)
    if value < 4:
        continue  # retry
    print(f"  Attempt {attempts}: got {value} (success!)")
    successes += 1

print(f"Got {successes} successes in {attempts} attempts")

In while loops, continue jumps back to the condition check. Make sure the loop still progresses to avoid infinite loops.

Want to try these examples interactively?

Open Intermediate Playground