Web Analytics

Loop Control Statements

Beginner ~15 min read

Loop control statements give you fine-grained control over loop execution. break exits the loop entirely, continue skips to the next iteration, and pass does nothing (but is syntactically useful).

Statement Effect Common Use
breakExit loop immediatelyFound what you're looking for
continueSkip to next iterationFilter/skip certain items
passDo nothingPlaceholder for future code

The break Statement

Use break to exit a loop immediately. Execution continues with the first statement after the loop. If the loop has an else clause, it is NOT executed.

Output
Click Run to execute your code
Performance Tip: Using break for early exit can significantly improve performance. When searching a million items for a value that's at position 42, break lets you stop after checking just 42 items instead of all million!

The continue Statement

Use continue to skip the rest of the current iteration and move to the next one. The loop doesn't end - it just skips ahead.

Output
Click Run to execute your code
Pro Tip: continue is great for "guard clauses" at the start of a loop. Check for invalid conditions and continue early, then write the main logic without deep nesting.

The pass Statement

The pass statement does nothing. It's a placeholder when Python syntax requires a statement but you don't want to execute any code.

Output
Click Run to execute your code
Don't Confuse pass and continue! pass does nothing and continues to the next line. continue skips the rest of the iteration. After pass, the code keeps running; after continue, it jumps to the next iteration.

Practical Examples

These statements are essential for real-world patterns like validation, filtering, and early-exit optimizations.

Output
Click Run to execute your code

Common Mistakes

1. Using break when you meant continue

# Wrong - exits the entire loop!
for item in items:
    if not is_valid(item):
        break  # Stops processing all items!

# Correct - skip invalid, process rest
for item in items:
    if not is_valid(item):
        continue  # Skip this item, continue with others

2. Confusing pass with continue

# These are different!
for i in range(3):
    if i == 1:
        pass
    print(i)  # Prints 0, 1, 2 - pass does nothing

for i in range(3):
    if i == 1:
        continue
    print(i)  # Prints 0, 2 - continue skips 1

3. Forgetting break only exits ONE loop

# break only exits the inner loop!
for i in range(3):
    for j in range(3):
        if j == 1:
            break  # Only breaks inner loop
    print(f"i={i}")  # Still runs for each i!

4. Unnecessary else after continue

# Unnecessary else
for num in numbers:
    if num % 2 == 0:
        continue
    else:  # This else is redundant!
        print(num)

# Cleaner
for num in numbers:
    if num % 2 == 0:
        continue
    print(num)  # Only reaches here if didn't continue

Exercise: Find First Even Number

Task: Find the first even number in a list.

Requirements:

  • Iterate through the list
  • Skip odd numbers with continue
  • Print and break when even number found
Output
Click Run to execute your code
Show Solution
numbers = [1, 3, 5, 7, 8, 9, 10]
found_even = None

for num in numbers:
    if num % 2 != 0:  # Odd number
        continue  # Skip to next
    # If we reach here, it's even
    found_even = num
    print(f"First even: {num}")
    break

if found_even is None:
    print("No even numbers found")

Summary

  • break: Exit the loop immediately, skip loop's else
  • continue: Skip rest of current iteration, go to next
  • pass: Do nothing (placeholder)
  • break in nested loops: Only exits the innermost loop
  • Use break for early exit (found item, reached limit)
  • Use continue for filtering/skipping invalid items

What's Next?

Now that you can control loop flow, let's learn about nested loops - loops inside loops! These are essential for working with 2D data like grids, matrices, and combinations.