Skip to main content

Python break, continue, pass: Controlling Loop Flow

Beginner20 min5 exercises65 XP
0/5 exercises

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.

Using break to exit a loop early
Loading editor...

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.

Finding the first match with break
Loading editor...

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.

Using continue to skip even numbers
Loading editor...

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.

Without continue (nested if)
for num in range(1, 11):
    if num % 2 != 0:
        print(num)
With continue (flat)
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.

Skipping invalid data with continue
Loading editor...

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.

Using pass as a placeholder
Loading editor...

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.

Using pass to explicitly ignore cases
Loading editor...

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.

for-else when the target IS found (break runs)
Loading editor...

The loop found 9, so break ran and the else block was skipped. The else only runs when the loop completes without breaking.

for-else when the target is NOT found (else runs)
Loading editor...

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.

Without for-else (needs a flag)
found = False
for num in numbers:
    if num == target:
        print(f"Found {target}!")
        found = True
        break
if not found:
    print(f"{target} not found.")
With for-else (cleaner)
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.

break, continue, and pass in action
Loading editor...

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.

Exercise 1: Find the First Negative Number
Write Code

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: -3
Loading editor...
Exercise 2: Print Without Vowels
Write Code

Use 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:

btfl

Don't forget to print an empty print() at the end for a clean newline.

Loading editor...
Exercise 3: Predict the Output
Predict Output

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.

Loading editor...
Exercise 4: Fix the Bug — Sum Until Negative
Fix the Bug

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: 15

Don't change anything except the one wrong keyword.

Loading editor...
Exercise 5: Password Checker with for-else
Write Code

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 accepted
Loading editor...

Summary: break, continue, pass

Here's a quick reference for all three loop control statements:
---------
breakExits the loop immediatelyFound what you're looking for, or hit a stop condition
continueSkips to the next iterationCurrent item doesn't need processing
passDoes nothing (placeholder)Empty block that you'll fill in later
for-elseelse runs if no break occurredSearch operations: "found it" vs "not found"

Key rules to remember:

  • break stops the ENTIRE loop, not just one iteration
  • continue skips only the CURRENT iteration — the loop keeps going
  • pass is purely a placeholder — it does nothing at all
  • break only exits the innermost loop in nested loops
  • The else in for-else runs when the loop completes WITHOUT a break
  • Don't confuse continue (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.