Learn Without Walls
← Previous Lesson Lesson 3 of 4 Next Lesson →

Lesson 5.3: break, continue, and pass

What You'll Learn

The break Statement

break immediately exits the loop, regardless of the loop condition. Execution continues with the code after the loop.

break: Exits the innermost loop immediately. Any remaining iterations are skipped.
# Search for a number in a list
numbers = [4, 7, 2, 9, 1, 5, 8]
target = 9

for num in numbers:
    if num == target:
        print(f"Found {target}!")
        break
    print(f"Checking {num}...")

print("Search complete.")
Checking 4... Checking 7... Checking 2... Found 9! Search complete.

Notice that once 9 was found, the loop stopped. It did not check 1, 5, or 8.

Example: First Even Number

numbers = [7, 3, 11, 8, 5, 2]

for num in numbers:
    if num % 2 == 0:
        print(f"First even number: {num}")
        break
First even number: 8

The continue Statement

continue skips the rest of the current iteration and jumps to the next one. The loop does not exit, it just moves on to the next item.

continue: Skips the remaining code in the current iteration and moves to the next iteration of the loop.
# Print only odd numbers
for i in range(1, 11):
    if i % 2 == 0:
        continue  # Skip even numbers
    print(i, end=" ")
1 3 5 7 9

Example: Skip Negative Numbers

numbers = [10, -3, 5, -7, 8, -1, 4]
total = 0

for num in numbers:
    if num < 0:
        print(f"Skipping {num}")
        continue
    total += num

print(f"Sum of positive numbers: {total}")
Skipping -3 Skipping -7 Skipping -1 Sum of positive numbers: 27

break vs. continue

The key difference: break exits the loop entirely, while continue skips to the next iteration.

# break: stops the entire loop
print("break example:")
for i in range(5):
    if i == 3:
        break
    print(i, end=" ")

print()  # New line

# continue: skips one iteration
print("continue example:")
for i in range(5):
    if i == 3:
        continue
    print(i, end=" ")
break example: 0 1 2 continue example: 0 1 2 4

The pass Statement

pass does absolutely nothing. It is a placeholder that lets you write syntactically correct code when you have not yet decided what to put in a block.

pass: A "do nothing" statement. Used as a placeholder where Python syntax requires a statement but you do not want any action.
# Using pass as a placeholder while developing
for i in range(10):
    if i % 2 == 0:
        pass  # TODO: handle even numbers later
    else:
        print(f"{i} is odd")
1 is odd 3 is odd 5 is odd 7 is odd 9 is odd

Without pass, an empty block would cause a SyntaxError. Think of it as saying "I know I need code here, but not yet."

# pass in an empty function (preview of Module 9)
def calculate_tax():
    pass  # Will implement later

# pass in an empty loop
while False:
    pass  # This loop never runs, but it's valid syntax

The else Clause on Loops

Python has a unique feature: you can put an else clause on a loop. The else block runs only if the loop completed normally (was not terminated by break).

# else runs because break was NOT triggered
for i in range(5):
    if i == 10:  # Never true
        break
else:
    print("Loop completed without break")
Loop completed without break
# else does NOT run because break WAS triggered
for i in range(5):
    if i == 3:
        print("Breaking at 3")
        break
else:
    print("This won't print")
Breaking at 3

Practical Use: Searching

# Check if a number is prime
number = 17

for i in range(2, number):
    if number % i == 0:
        print(f"{number} is not prime (divisible by {i})")
        break
else:
    print(f"{number} is prime!")
17 is prime!

When to Use Each Statement

Example: Processing Data with All Three

data = [5, -2, 0, 8, 999, 3]

for value in data:
    if value == 999:
        print("End of data marker found")
        break         # Stop processing

    if value < 0:
        continue      # Skip negative values

    if value == 0:
        pass          # TODO: handle zero case
    else:
        print(f"Processing: {value}")
Processing: 5 Processing: 8 End of data marker found

Check Your Understanding

What will this code print?

for i in range(1, 6):
    if i == 2:
        continue
    if i == 4:
        break
    print(i)

Answer:

1
3

i=1: no skip, no break, prints 1.
i=2: continue skips the rest, does not print.
i=3: no skip, no break, prints 3.
i=4: break exits the loop. 4 and 5 never print.

Key Takeaways

← while Loops Lesson 3 of 4 Next: Nested Loops →