Break, Continue & Pass
Learn how to control loop execution in Python using the break, continue, and pass statements.
Introduction
Loops normally run until their condition becomes false or they run out of items to iterate over. But sometimes you need finer control — you might want to stop a loop the moment you find what you are looking for, skip certain values without stopping the loop, or leave a block of code temporarily empty while you plan it out.
Python gives you three small but powerful statements for exactly this: break, continue, and pass. Each one changes the normal flow of a loop (or, in the case of pass, a block of code) in a very specific way.
- How break exits a loop immediately.
- How continue skips the rest of the current iteration.
- How pass acts as a "do nothing" placeholder.
- How to combine break and continue in a real search example.
The break Statement
The break statement immediately exits the nearest enclosing loop, no matter what the loop condition says. Execution jumps straight to the first line of code after the loop.
for number in range(1, 10): if number == 5: break print(number)
print("Loop ended")Click Run to see what this code prints.
As soon as number equals 5, break runs and the loop stops completely — 5, 6, 7, 8, and 9 are never printed.
Immediate Exit
break stops the loop instantly, skipping any remaining iterations.
Common Use
Searching for an item and stopping as soon as it is found.
Works in Both Loops
break works the same way inside for loops and while loops.
count = 0while True: count += 1 if count > 3: break print("Count is", count)Click Run to see what this code prints.
This pattern — while True combined with a break condition inside — is extremely common for loops that should run "until something happens" rather than a fixed number of times.
The continue Statement
The continue statement skips the rest of the current iteration and jumps straight to the next one. Unlike break, the loop itself does not stop — it simply moves on early.
for number in range(1, 6): if number == 3: continue print(number)Click Run to see what this code prints.
When number equals 3, continue is triggered, so the print(number) line is skipped just for that iteration — but the loop keeps going with 4 and 5.
Skip, Not Stop
continue skips only the current pass through the loop body.
Loop Continues
The loop returns to its condition check (or next item) as normal.
Filtering Values
Often used to ignore values that do not meet some condition.
numbers = [4, -1, 7, -3, 9, 0]
for n in numbers: if n < 0: continue print("Positive value:", n)Click Run to see what this code prints.
The pass Statement
The pass statement does absolutely nothing — it is a no-op. Python's syntax requires every block (after if, for, while, def, class, and so on) to contain at least one statement. When you are not ready to write that code yet, pass acts as a placeholder that keeps the syntax valid.
for number in range(5): if number == 2: pass # TODO: handle this case later print(number)Click Run to see what this code prints.
Notice that pass changes nothing about the output — every number still prints, including 2. pass truly does nothing; it just satisfies Python's requirement that a block cannot be empty.
Stub Functions
Write a function signature now and fill in the logic later.
Stub Classes
Define an empty class as a placeholder before adding attributes and methods.
Empty Blocks
Satisfy Python's syntax when an if/for/while block should do nothing yet.
def calculate_discount(price): pass # not implemented yet
class Customer: pass # attributes and methods coming soon
print("File loads without errors")Click Run to see what this code prints.
- Writing an if block with nothing inside it is a SyntaxError.
- pass is a real statement that satisfies Python's "a block must contain code" rule.
- It is commonly used while sketching out a program's structure before filling in details.
Combining break and continue in a Search
Real programs often use break and continue together. Here is a small program that searches a list of usernames, skips over blank entries with continue, and stops immediately once it finds a match using break.
usernames = ["alice", "", "bob", "charlie", "", "diana"]target = "charlie"found = False
for name in usernames: if name == "": continue # skip blank entries
print("Checking:", name)
if name == target: found = True break # stop as soon as we find it
if found: print(f"'{target}' was found!")else: print(f"'{target}' was not found.")Click Run to see what this code prints.
The loop skips the two blank strings entirely thanks to continue, prints each real username as it checks it, and stops the instant it finds "charlie" thanks to break — never even looking at "diana".
Common Mistakes
- Confusing break and continue — break exits the loop entirely, continue only skips one iteration.
- Using pass when you meant continue, or vice versa — they do very different things.
- Forgetting that break only exits the innermost loop, not all nested loops at once.
- Leaving pass in place permanently instead of replacing it with real logic before shipping code.
- Placing break or continue outside of any loop, which raises a SyntaxError.
Best Practices
- Use break when you have found what you need and further looping is wasted work.
- Use continue to skip invalid or irrelevant items instead of nesting deep if/else blocks.
- Use pass as a temporary stub, and leave a comment or TODO reminding yourself to finish it.
- Prefer a clear if condition with continue over deeply indented nested logic.
- Keep loops readable — if you need break and continue in many places, consider a function instead.
Frequently Asked Questions
Yes. break works identically in for loops and while loops — it immediately exits whichever loop directly contains it.
No. continue only skips the rest of the current iteration. The loop moves on to its next item or condition check as normal.
pass does nothing at all — code after it in the same iteration still runs. continue actively skips the remaining code in that iteration. They are not interchangeable.
No, break only exits the innermost loop it is directly inside. To exit multiple nested loops, you typically use a flag variable or restructure the code into a function and return.
Yes. pass is very commonly used in empty function bodies, empty class bodies, and empty exception handlers while code is still being planned or written.
Key Takeaways
- break immediately exits the nearest enclosing loop.
- continue skips the rest of the current iteration and moves to the next one.
- pass does nothing — it is a placeholder that keeps Python's syntax valid.
- break and continue only affect the loop they are directly written inside.
- These three statements give you fine-grained control over how loops execute.
Summary
break, continue, and pass are small statements with big effects on how your loops behave. break stops a loop entirely, continue skips ahead to the next iteration, and pass is a harmless placeholder for code you have not written yet.
With these tools, you can write loops that search, filter, and stub out logic cleanly. Next, you will learn how to package reusable logic into functions.
- You can exit a loop early with break.
- You can skip an iteration with continue.
- You understand why pass is needed as a placeholder.
- You are ready to start writing your own functions.