Python break, continue, pass: Controlling Loop Flow
You're searching through a phone book for your friend's number. Once you find it, do you keep flipping through every remaining page? Of course not — you stop. Or imagine you're reading a list of emails. You skip the spam and only read the real ones. These are everyday examples of controlling the flow of a loop.
Python gives you three statements to control what happens inside a loop: break (stop the loop entirely), continue (skip to the next iteration), and pass (do nothing, just hold space). This tutorial covers all three, plus Python's unique for-else pattern.
By the end, you'll know exactly when to use each statement and how they change the way your loops behave.
How Does break Work in Python?
The break statement immediately exits the loop it's inside. Python jumps to the first line of code after the loop. No more iterations, no more condition checks — the loop is done.
Think of break as an emergency exit. The loop has a normal exit (the condition becomes False or the items run out), but break lets you leave early when you've found what you need.
Without break, the loop would check Diana and Eve too — wasted effort since we already found Charlie. The break statement saves time by stopping as soon as the job is done.
The loop stops at 8 because it's the first even number. Even though 2 is also even, the loop never reaches it. This "find the first match and stop" pattern is one of the most common uses of break.
How Does continue Work in Python?
The continue statement skips the rest of the current iteration and jumps back to the top of the loop. The loop itself keeps running — it just moves on to the next item (or checks the condition again for while loops).
If break is an emergency exit, continue is more like a "skip this one" button. You're still going through the loop — you just don't want to process this particular item.
When num is even (2, 4, 6, 8, 10), continue skips the print() and jumps to the next iteration. When num is odd, continue doesn't run, so print() executes normally.
for num in range(1, 11):
if num % 2 != 0:
print(num)for num in range(1, 11):
if num % 2 == 0:
continue
print(num)Both versions produce the same output. For this simple example, the if version is fine. But continue really shines when you have many lines of code after the skip condition — it keeps your code flat instead of deeply indented.
Here, continue acts as a filter. Invalid scores (negative or over 100) get skipped, so the calculation lines never run for them. Only valid scores get added to the total. This is cleaner than wrapping all the processing code in a big if block.
What Does pass Do in Python?
The pass statement does absolutely nothing. It's a placeholder that says "I know I need code here, but I haven't written it yet." Python requires at least one statement in any block (after if, for, while, function definitions, etc.), and pass fills that requirement.
Think of pass as a "TODO" sign on a construction site. The building isn't finished yet, but the structure is in place. It tells both Python and other programmers that this is intentionally empty.
Without pass, Python would throw an IndentationError because it expects an indented block after each colon. The pass statement satisfies this requirement while doing nothing.
In this example, pass makes it clear that we're deliberately ignoring non-letter characters. You could leave out the else: pass entirely and the program would work the same way, but including it communicates your intent to other readers.
What Is the for-else Pattern in Python?
Python has a unique feature that most other languages don't: you can attach an else block to a for or while loop. The else block runs only if the loop completed normally — meaning it was NOT interrupted by a break.
This is confusing at first because else usually means "if the condition was False." But for loops, think of it as "else no break happened." If the loop finishes all its iterations without hitting break, the else runs.
The loop found 9, so break ran and the else block was skipped. The else only runs when the loop completes without breaking.
This time the loop checked every number and never found 6. Since break never ran, the else block executed. This replaces the common pattern of using a boolean flag like found = False.
found = False
for num in numbers:
if num == target:
print(f"Found {target}!")
found = True
break
if not found:
print(f"{target} not found.")for num in numbers:
if num == target:
print(f"Found {target}!")
break
else:
print(f"{target} not found.")The for-else version eliminates the need for a found flag. It's a more Pythonic way to handle "search and report" patterns.
Real-World Use Cases for break, continue, and pass
Let's see how these three statements work together in a more realistic example. This code processes a batch of orders and handles different situations.
Order #1 (Laptop) is pending, so it gets processed. Order #2 (Mouse) is shipped, so continue skips it. Order #3 (Keyboard) is cancelled, also skipped. Order #4 (Monitor) is pending, so it gets processed. Order #5 is a STOP command, so break ends the loop entirely. Order #6 never gets reached.
This is the typical pattern: use break for "stop everything" conditions, continue for "skip this one" conditions, and the rest of the loop body for normal processing.
Practice Exercises
Time to practice controlling loop flow! These exercises will test your understanding of when to use break, continue, and the for-else pattern.
Pay close attention to what each exercise asks for. Using break when you need continue (or vice versa) is a common mistake.
Use a for loop with break to find and print the first negative number in the list. Print "Found: " followed by the number.
Expected output:
Found: -3Use a for loop with continue to print all the characters in the string "beautiful", but skip the vowels (a, e, i, o, u). Print each consonant on the same line using end="".
Expected output:
btflDon't forget to print an empty print() at the end for a clean newline.
Without running the code, predict exactly what it will print. Trace through each iteration and think about when continue and break trigger.
Then run it to check your answer.
This code should add up numbers from the list until it hits a negative number, then stop. But someone used continue instead of break. Fix the bug so it prints:
Sum before negative: 15Don't change anything except the one wrong keyword.
Use a for-else pattern to check if a password contains at least one digit. Loop through each character in the password. If you find a digit, print "Password accepted" and break. If the loop finishes without finding a digit, print "Password must contain a number".
Expected output for the given password:
Password acceptedSummary: break, continue, pass
| Here's a quick reference for all three loop control statements: | ||
|---|---|---|
| --- | --- | --- |
break | Exits the loop immediately | Found what you're looking for, or hit a stop condition |
continue | Skips to the next iteration | Current item doesn't need processing |
pass | Does nothing (placeholder) | Empty block that you'll fill in later |
for-else | else runs if no break occurred | Search operations: "found it" vs "not found" |
Key rules to remember:
break stops the ENTIRE loop, not just one iterationcontinue skips only the CURRENT iteration — the loop keeps goingpass is purely a placeholder — it does nothing at allbreak only exits the innermost loop in nested loopselse in for-else runs when the loop completes WITHOUT a breakcontinue (skip one) with break (stop all)What's Next?
Now you know how to control loop flow with precision. Next up: [Python Nested Loops](/python/python-nested-loops) — where you'll learn to put loops inside loops for working with grids, combinations, and multi-dimensional data. Nested loops combined with break and continue unlock powerful patterns.