Skip to content

Python break, continue and pass

Updated 6 min read Aimerscrew

Loops normally run from start to finish. The Python break, continue and pass statements let you change that from inside the loop: stop early, skip a turn, or simply hold a place. They're tiny keywords, but you'll use the first two in almost every real program you write.

🌍 Real-life example

Imagine you're looking for your friend Chloe in your phone's contact list. You scroll down name by name, and the moment you see "Chloe" you stop scrolling. That's break. Now imagine packing groceries from a list: when an item is out of stock, you skip it and move to the next one. That's continue.

What does break do in Python?

The break statement ends the loop immediately. Python jumps straight to the first line after the loop, even if there are items left or the condition is still true.

contacts = ["Amara", "Ben", "Chloe", "Diego", "Emma"]

for name in contacts:
    print(f"Checking {name}...")
    if name == "Chloe":
        print("Found Chloe!")
        break

print("Search finished.")
Checking Amara...
Checking Ben...
Checking Chloe...
Found Chloe!
Search finished.

Diego and Emma are never checked. There's no point looking further once the search has succeeded, so break saves time.

break in a while loop

In the while loops lesson you saw while True:, a loop that only ends with break. Here's another example: adding up prices until the cashier types 0.

total = 0

while True:
    price = float(input("Item price (0 to finish): "))
    if price == 0:
        break
    total += price

print(f"Total: ${total:.2f}")

The user typed 4.50, 12.25 and then 0:

Item price (0 to finish): 4.50
Item price (0 to finish): 12.25
Item price (0 to finish): 0
Total: $16.75

Notice break comes before total += price, so the final 0 is never added.

What does continue do in Python?

The continue statement skips the rest of the current iteration and jumps back to the top of the loop for the next one. The loop itself keeps going.

shopping_list = ["bread", "milk", "avocados", "eggs", "basil"]
out_of_stock = ["avocados", "basil"]

for item in shopping_list:
    if item in out_of_stock:
        print(f"Skipping {item} (out of stock)")
        continue
    print(f"Added {item} to the bag")
Added bread to the bag
Added milk to the bag
Skipping avocados (out of stock)
Added eggs to the bag
Skipping basil (out of stock)

When an item is out of stock, continue skips the "Added" line for that item only. Every item still gets its turn.

Animation comparing break and continue: on the left the loop stops for good at Chloe and never checks Diego or Emma; on the right the loop skips eggs and keeps going to the end of the list
break leaves the loop for good. continue skips one item and carries on with the next.

continue in a while loop

With a while loop, be sure the counter changes before continue. This loop prints the odd numbers from 1 to 9:

n = 0
while n < 9:
    n += 1
    if n % 2 == 0:
        continue
    print(n, end=" ")
1 3 5 7 9

The modulo check n % 2 == 0 (from arithmetic operators) is true for even numbers, so they're skipped.

What does pass do in Python?

The pass statement does nothing. It exists because Python needs at least one statement inside every indented block. If you want to sketch out a loop, an if or a function before you've written its body, pass keeps the code valid.

for order in ["latte", "mocha"]:
    pass  # TODO: send orders to the kitchen

print("Program still runs fine.")
Program still runs fine.

Without pass, an empty block is an error:

temperature = 30
if temperature > 25:

print("Done")
IndentationError: expected an indented block after 'if' statement on line 2

pass vs continue: they're not the same

Beginners often mix these up. pass does nothing and lets the code below it run. continue jumps to the next iteration and skips the code below it.

for n in [1, 2, 3]:
    if n == 2:
        pass
    print("pass loop:", n)

for n in [1, 2, 3]:
    if n == 2:
        continue
    print("continue loop:", n)
pass loop: 1
pass loop: 2
pass loop: 3
continue loop: 1
continue loop: 3

break vs continue vs pass at a glance

StatementWhat it doesLoop keeps going?Typical use
breakExits the loop immediatelyNoStop once you've found what you need
continueSkips to the next iterationYesIgnore items you don't want to process
passNothing; it's a placeholderYes (no effect)Empty blocks you'll fill in later

Does break exit all loops?

No. In nested loops (a loop inside another loop), break only ends the innermost loop, the one it's directly inside. The outer loop carries on.

for row in ["A", "B", "C"]:
    for seat in range(1, 4):
        if seat == 2:
            break
        print(f"Seat {row}{seat}")
Seat A1
Seat B1
Seat C1

Each time the inner loop reaches seat 2, it stops, but the outer loop moves on to the next row. To stop both loops, you'd typically use a flag variable or put the loops in a function and return from it.

Real-world example: finding the first available flight

This program combines both statements. It skips fully booked flights with continue and stops at the first one that's cheap enough with break.

flights = [
    ("BA117", 640, 0),
    ("UA901", 410, 12),
    ("AF023", 520, 3),
    ("DL044", 380, 7),
]
budget = 450

for code, price, seats_left in flights:
    if seats_left == 0:
        print(f"{code}: fully booked, skipping")
        continue
    if price <= budget:
        print(f"Book {code} for ${price}!")
        break
    print(f"{code}: ${price} is over budget")
BA117: fully booked, skipping
Book UA901 for $410!

The flights are stored as small groups of three values called tuples, unpacked into three names in the for line. You'll learn about them in Python tuples. DL044 is even cheaper, but the loop never reaches it: break takes the first match.

Common mistakes

1. Using break outside a loop

age = 15
if age < 18:
    break
SyntaxError: 'break' outside loop

break and continue only work inside a for or while loop, not inside a plain if.

2. continue before the counter update in a while loop

n = 0
while n < 5:
    if n == 2:
        continue
    print(n)
    n += 1

When n is 2, continue jumps back to the top before n += 1 runs, so n stays 2 forever: an infinite loop. Move the update above the continue.

3. Expecting pass to skip the rest of the loop

As the side-by-side example showed, pass doesn't skip anything. If you want to skip, use continue.

4. Wrong indentation for break

for name in ["Amara", "Ben", "Chloe"]:
    if name == "Chloe":
        print("Found her!")
    break

This prints nothing at all. The break isn't inside the if, so it runs on the very first item and ends the loop after checking only Amara.

Practice exercise

🏋️ Try it yourself

A weather station sends these temperature readings: [12, 15, -99, 14, 0, 18]. The value -99 means a sensor error and should be skipped. The value 0 means "end of data", so stop reading there. Add up the valid readings and print the total. (Expected total: 41.)

Show solution
readings = [12, 15, -99, 14, 0, 18]
total = 0

for reading in readings:
    if reading == -99:
        continue
    if reading == 0:
        break
    total += reading

print(f"Total of valid readings: {total}")
Total of valid readings: 41

12 + 15 + 14 = 41. The error value is skipped, and 18 is never reached because the loop breaks at 0.

What's next?

There's one more loop feature that only makes sense once you know break: an else block that runs when a loop finishes without breaking. Learn it in the Python loop else clause.

Key takeaways

  • break ends the loop immediately and jumps to the first line after it.
  • continue skips the rest of the current iteration and moves on to the next one.
  • pass does nothing; it is a placeholder for a block you have not written yet.
  • In nested loops, break and continue only affect the innermost loop.
  • In a while loop, update the counter before continue, or you may create an infinite loop.
  • break and continue only work inside loops; using break in a plain if is a SyntaxError.

Frequently asked questions

What is the difference between break and continue in Python?

break stops the whole loop and moves on to the code after it. continue only skips the rest of the current iteration, and the loop carries on with the next item.

What is pass used for in Python?

pass is a statement that does nothing. It is used as a placeholder where Python requires a statement, such as an empty loop, if block, function or class that you plan to fill in later.

Does break stop all nested loops in Python?

No. break only exits the loop it is directly inside. The outer loop keeps running. To stop several loops at once, use a flag variable or return from a function.

Can you use break in an if statement?

Only when the if statement is inside a loop. break always exits a loop, so using it in an if that is not inside a for or while loop gives SyntaxError: break outside loop.

Is pass the same as continue in Python?

No. pass does nothing and the code after it still runs. continue skips the remaining code in that iteration and jumps to the next one.