By WebNest Studio Engineering Team
Python Tutorial
break, continue and pass in Python
Sometimes a loop should stop early, skip an item, or do nothing at all for now. Python gives you three small statements for this: break exits the loop, continue skips to the next iteration, and pass is a placeholder that does nothing.
This lesson explains each statement, the difference between break and continue, how they behave in nested loops, and where pass is useful.
Why it matters
Loops rarely run from the first item to the last without exception. You stop as soon as you have found what you were looking for, and you skip items that do not apply. break and continue express both directly, and they keep a loop from doing work that is no longer needed.
break
break immediately ends the innermost loop; execution continues after the loop, and any loop else block is skipped. Typical uses: stop searching once you find something, exit a while True loop when the user enters "quit".
continue
continue skips the rest of the current iteration and jumps to the next one. It keeps loop bodies flat: filter out invalid items at the top with if bad: continue instead of wrapping the rest in an if.
pass
pass does nothing. Python requires at least one statement in a block, so pass is used as a placeholder for functions or classes you will implement later, empty exception handlers (use rarely!), or minimal class definitions. ... (Ellipsis) is often used the same way in stubs.
break vs continue in Nested Loops
Both affect only the innermost loop. To exit several loops at once, move the loops into a function and return, or use a flag variable.
Examples
break and continue side by side
print("break:")
for n in range(1, 10):
if n == 5:
break
print(n, end=" ")
print()
print("continue:")
for n in range(1, 10):
if n % 3 == 0:
continue
print(n, end=" ")
print()
break:
1 2 3 4
continue:
1 2 4 5 7 8
A menu loop with while True and break
commands = ["add", "list", "oops", "quit", "never reached"]
cart = []
for cmd in commands:
if cmd == "quit":
print("Bye!")
break
if cmd not in ("add", "list"):
print(f"Unknown command: {cmd}")
continue
if cmd == "add":
cart.append("item")
print(cmd, "->", cart)
add -> ['item']
list -> ['item']
Unknown command: oops
Bye!
pass as a placeholder, and exiting nested loops with return
class PaymentGateway:
pass # to be implemented later
def todo():
pass
def find_pair(numbers, target):
for i, a in enumerate(numbers):
for b in numbers[i + 1:]:
if a + b == target:
return a, b # leaves both loops
return None
print(type(PaymentGateway()).__name__, todo())
print(find_pair([2, 7, 11, 15], 18))
print(find_pair([1, 2, 3], 100))
PaymentGateway None
(7, 11)
None
Common Mistakes
- Expecting break to exit all nested loops — it only exits the innermost one.
- Using continue in a while loop before incrementing the counter, creating an infinite loop.
- Using except: pass to silence errors, hiding real bugs.
- Confusing pass (do nothing, keep going) with continue (skip to the next iteration).
Exercise
Use break to print the first number in the first list that is divisible by 7. Then use continue to add up only the positive numbers in the second list, and print the total.
Expected output: 14 then 16
numbers = [3, 10, 14, 21, 5]
# TODO: print the first number divisible by 7, then stop looking
values = [4, -2, 7, -1, 5]
total = 0
# TODO: add up the values, skipping the negative ones with continue
print(total)
Show hints
- After printing the number,
breakends the loop, so 21 is never reached. continuejumps to the next item, so the line that adds to the total is skipped for negative values.
Show solution
numbers = [3, 10, 14, 21, 5]
for number in numbers:
if number % 7 == 0:
print(number)
break
values = [4, -2, 7, -1, 5]
total = 0
for value in values:
if value < 0:
continue
total += value
print(total)
Quick quiz
1. In a loop inside another loop, what does break in the inner loop do?
- A. Ends both loops
- B. Ends the program
- C. Ends only the inner loop
- D. Skips one iteration
Show answer
C. break leaves the innermost loop that contains it. The outer loop carries on.
2. What does pass do?
- A. Skips to the next iteration
- B. Nothing; it fills a place where a statement is required
- C. Exits the function
- D. Raises an error
Show answer
B. It is used for an empty function, class or block that will be filled in later.
3. What does continue do?
- A. Ends the loop
- B. Does nothing
- C. Restarts the loop from the beginning
- D. Skips the rest of the current iteration and moves to the next one
Show answer
D. The loop itself goes on with the next item.
Interview questions
What is the difference between break, continue and pass?
Show answer
break ends the nearest enclosing loop immediately. continue ends only the current pass through the loop and goes on to the next item. pass does nothing at all; it exists because Python requires at least one statement in a block.
How do you exit from several nested loops at once?
Show answer
Python has no labelled break. The cleanest way is to put the loops in a function and use return, which leaves all of them. The alternatives are a flag variable that each outer loop checks, or raising and catching an exception, both of which are harder to read.
Key Points to Remember
- break exits the innermost loop and skips its else clause.
- continue skips the rest of the current iteration.
- pass is a no-op placeholder required where a statement is syntactically needed.
- Use a function with return to leave several nested loops at once.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering