break, continue & pass
Learn how to control the execution of Python loops using
break, continue, and
pass. These statements give you precise
control over what happens inside loops and conditional
blocks.
What You Will Learn
- Understand how loop control statements work.
- Use
breakto terminate loops. - Use
continueto skip iterations. - Understand the purpose of
pass. - Control nested loops carefully.
- Use loop control statements in real programs.
- Understand how
breakinteracts with loopelse. - Avoid common control-flow mistakes.
Controlling Loops
In the previous lessons, you learned how
for and while loops repeat
code.
Sometimes, however, simply repeating the same block of code is not enough. A program may need to:
- Stop a loop immediately.
- Skip a particular iteration.
- Leave a block temporarily empty.
Python provides three useful statements for these situations:
break
continue
pass
Simple Mental Model
- break → Stop the loop.
- continue → Skip this iteration.
- pass → Do nothing.
The break Statement
The break statement immediately terminates
the nearest enclosing loop.
for number in range(1, 11):
if number == 5:
break
print(number)
1 2 3 4
When number becomes 5, Python encounters
break and immediately exits the loop.
break does not pause the loop.
It completely terminates the current loop.
break with for
A for loop normally continues until all
items have been processed. break lets you
stop early when a particular condition is reached.
for number in [10, 20, 30, 40, 50]:
if number == 30:
break
print(number)
10 20
The values after 30 are never processed because the loop ends when 30 is encountered.
Stop When a Condition Is Met
names = ["Ali", "Sara", "Ahmed", "Fatima"]
for name in names:
if name == "Ahmed":
break
print(name)
Ali Sara
break with while
break is particularly useful in
while loops when you want to create a
loop that runs until an explicit exit condition occurs.
number = 1
while True:
print(number)
if number == 5:
break
number += 1
1 2 3 4 5
Here, while True creates a loop that would
otherwise continue forever. The break
statement provides the exit mechanism.
Common Pattern
The combination of
while True and break
is often used for menu-driven programs,
command processors and repeated user input.
Searching with break
One of the most practical uses of break is
stopping a search as soon as the desired item is found.
products = ["Laptop", "Mouse", "Keyboard", "Monitor"]
search = "Keyboard"
for product in products:
if product == search:
print("Product found!")
break
Product found!
Once the product is found, continuing through the rest of the list would be unnecessary.
Search with a Result Variable
numbers = [12, 18, 25, 31, 44]
target = 31
found = False
for number in numbers:
if number == target:
found = True
break
print("Found:", found)
Found: True
The continue Statement
The continue statement skips the remaining
code in the current iteration and moves to the next
iteration of the loop.
for number in range(1, 6):
if number == 3:
continue
print(number)
1 2 4 5
When the number is 3, Python skips the
print() statement and continues with
the next iteration.
continue does not terminate the
loop. It only skips the current iteration.
continue with for
continue is useful when you want to ignore
certain items but continue processing the rest.
Print Only Odd Numbers
for number in range(1, 11):
if number % 2 == 0:
continue
print(number)
1 3 5 7 9
Skip Invalid Values
values = [10, -5, 20, -2, 30]
for value in values:
if value < 0:
continue
print(value)
10 20 30
continue with while
continue can also be used inside a
while loop, but you must carefully manage
the loop-control variable.
number = 0
while number < 6:
number += 1
if number == 3:
continue
print(number)
1 2 4 5 6
Notice that number += 1 happens before
continue. This ensures that the loop
continues making progress.
If a continue statement skips the
code responsible for updating your loop variable,
the loop can become infinite.
The pass Statement
The pass statement does nothing.
It is used when Python requires a statement but you do not want the program to perform an action yet.
for number in range(5):
if number == 3:
pass
print(number)
0 1 2 3 4
Unlike continue, pass does
not skip the rest of the loop iteration. The next
statement still executes.
Important Difference
pass is simply a placeholder.
It does not change the normal flow of execution.
When to Use pass
The most common purpose of pass is to create
a syntactically valid placeholder while you plan the
implementation.
Placeholder Function
def calculate_salary():
pass
Without pass, the function would have no
valid body.
Placeholder Class
class Customer:
pass
This creates a valid empty class that can be developed later.
Placeholder Conditional Block
age = 25
if age >= 18:
pass
else:
print("Underage")
The if block intentionally performs no
action.
pass means "do nothing here."
continue means "skip the rest of
this iteration and move to the next one."
break vs continue vs pass
Understanding the difference between these three statements is extremely important.
break → Exit the loop completely
continue → Skip the current iteration
pass → Do nothing
Visualizing the Difference
for number in range(5):
if number == 2:
break
print(number)
0 1
for number in range(5):
if number == 2:
continue
print(number)
0 1 3 4
for number in range(5):
if number == 2:
pass
print(number)
0 1 2 3 4
Control Statements in Nested Loops
When loops are nested, break and
continue affect the nearest
enclosing loop.
for outer in range(3):
for inner in range(3):
if inner == 1:
break
print(outer, inner)
0 0 1 0 2 0
The break statement terminates only the
inner loop. The outer loop continues normally.
Important Rule
A break statement does not
automatically terminate every nested loop.
It exits only the nearest enclosing loop.
Loop else and break
Python's loop else block is especially
useful when combined with break.
The else block executes when the loop
finishes normally. It does not execute if the loop
is terminated by break.
numbers = [10, 20, 30, 40]
target = 25
for number in numbers:
if number == target:
print("Found")
break
else:
print("Not found")
Not found
Because 25 was never found, the loop completed normally
and the else block executed.
This pattern is excellent for search operations:
break when the item is found and
else when the search finishes
without finding it.
Practical Real-World Examples
Example 1 — Stop at a Failed Transaction
transactions = [1000, 2500, -1, 3000, 4500]
for amount in transactions:
if amount < 0:
print("Invalid transaction found.")
break
print("Processing:", amount)
Processing: 1000 Processing: 2500 Invalid transaction found.
Example 2 — Skip Invalid Marks
marks = [78, -1, 91, 65, -5, 88]
for mark in marks:
if mark < 0:
continue
print("Valid mark:", mark)
Valid mark: 78 Valid mark: 91 Valid mark: 65 Valid mark: 88
Example 3 — Login Attempts
correct_password = "python123"
for attempt in range(3):
password = input("Enter password: ")
if password == correct_password:
print("Login successful.")
break
print("Incorrect password.")
else:
print("Account locked.")
Example 4 — Processing Orders
orders = [
"Order #101",
"Order #102",
"INVALID",
"Order #104"
]
for order in orders:
if order == "INVALID":
print("Invalid order detected.")
continue
print("Processing:", order)
Processing: Order #101 Processing: Order #102 Processing: Order #104
Example 5 — Placeholder for Future Logic
for feature in ["login", "payments", "reports"]:
if feature == "payments":
pass
print("Feature:", feature)
The pass statement allows the block to
remain empty while keeping the program syntactically
valid.
Common Mistakes
1. Thinking continue Ends the Loop
This is incorrect. continue only skips
the current iteration.
Use break when you want to exit
the loop. Use continue when you
want to skip one iteration.
2. Thinking pass Skips an Iteration
pass does not skip anything.
It simply performs no operation.
3. Creating an Infinite Loop with continue
number = 0
while number < 5:
if number == 2:
continue
number += 1
When number becomes 2, the program reaches
continue before increasing the number.
The condition remains true forever.
4. Expecting break to Exit All Nested Loops
A single break only exits the nearest
enclosing loop.
5. Using pass When Real Logic Is Required
pass is useful as a placeholder, but it
should not be used to hide unfinished logic in a
completed program.
Practice Exercises
Exercise 1 — Stop at 5
Use a for loop and break
to print numbers from 1 to 10 but stop when the
number reaches 5.
Exercise 2 — Skip Even Numbers
Use continue to print only odd numbers
between 1 and 20.
Exercise 3 — Search a List
Search a list of names. Stop searching when a particular name is found.
Exercise 4 — Invalid Data
Given a list of numbers, skip negative values and print only valid positive values.
Exercise 5 — Password Attempts
Give the user three chances to enter the correct
password. Use break after successful
login.
Exercise 6 — Use pass
Create an empty function and use pass
as its placeholder body.
Exercise 7 — Search with for...else
Search for a number in a list and display
"Found" or "Not found" using break
and else.
Exercise 8 — Nested Loops
Create nested loops and use break to
stop only the inner loop.
Mini Challenge — Smart Number Search
Build a program that searches a list of numbers and intelligently handles the data.
Your program should:
- Store a list of numbers.
- Ask the user for a number to search for.
- Ignore negative values using
continue. - Stop searching when the target is found using
break. - Use
for...elseto display a message if the target is not found. - Count how many valid numbers were checked.
numbers = [10, -5, 20, 35, -2, 40, 55]
target = int(input("Enter number to search: "))
checked = 0
for number in numbers:
if number < 0:
continue
checked += 1
if number == target:
print("Number found!")
break
else:
print("Number not found.")
print("Valid numbers checked:", checked)
Upgrade the Challenge
Modify the program so that it reports the position of the target number when it is found.
The goal is not just to make the program work.
Understand exactly why each of
continue, break and
else is being used.
Quick Knowledge Check
1. What does break do?
2. What does continue do?
3. What does pass do?
4. Which statement should you use to stop a loop?
5. What happens when continue executes?
6. What does a single break do inside nested loops?
7. When does a loop's else block normally execute?
8. Which statement is commonly used as a placeholder?
Lesson Summary
Python provides three important statements for controlling
program flow: break, continue,
and pass.
breakimmediately exits the nearest loop.continueskips the current iteration.passperforms no operation.breakis useful for searches and early exits.continueis useful for filtering or skipping unwanted data.passis useful as a placeholder for future code.- In nested loops,
breakaffects only the nearest loop. - Loop
elseruns when a loop completes normally withoutbreak. - Careless use of
continueinsidewhileloops can create infinite loops.
You can now decide not only how loops repeat, but also exactly when iterations should be skipped, stopped, or intentionally left empty.
Ready for the Next Lesson?
Complete the exercises and Smart Number Search challenge, then mark this lesson as complete.