Lesson 5.3: break, continue, and pass
What You'll Learn
- How to exit a loop early with
break - How to skip iterations with
continue - How to use
passas a placeholder - When each statement is appropriate
- The
elseclause on loops
The break Statement
break immediately exits the loop, regardless of the loop condition. Execution
continues with the code after the loop.
# 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.")
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
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.
# Print only odd numbers for i in range(1, 11): if i % 2 == 0: continue # Skip even numbers print(i, end=" ")
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}")
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=" ")
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.
# 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")
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")
# 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")
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!")
When to Use Each Statement
break: When you have found what you are looking for and want to stop immediately (searching, validation with early exit)continue: When you want to skip certain items but keep processing the rest (filtering, ignoring invalid data)pass: When you need a placeholder for code you have not written yet (development, empty blocks)
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}")
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
breakexits the loop entirelycontinueskips to the next iterationpassdoes nothing (placeholder)- Loop
elseruns only if the loop was not broken - Use
breakfor searching;continuefor filtering;passfor placeholders