Control Flow

Loop Control

Break: Exit Immediately

Stop the loop when you find what you need:

for num in range(1, 100):
    if num == 7:
        print("Found 7!")
        break
    print(num)
# Prints: 1, 2, 3, 4, 5, 6, Found 7!

Continue: Skip Current Iteration

Skip certain items without stopping:

for num in range(1, 6):
    if num == 3:
        continue  # Skip 3
    print(num)
# Prints: 1, 2, 4, 5

Real-World Example

# Skip invalid data
scores = [85, -1, 92, 78, -1, 95]

for score in scores:
    if score < 0:
        continue  # Skip invalid scores
    print(f"Valid score: {score}")
Visualizer
$ python find_even.py
Checking: 1 Checking: 3 Checking: 5 Found even number: 4

Interactive Visualization

python
Loading...
Output
Run execution to see output...