PYTHON BASICS • LESSON 15
break, continue & pass
Learn how to control Python loops using break, continue, and pass. These statements help you stop, skip, or temporarily leave loop logic.
1. Why Control a Loop?
Normally, a loop continues until its normal stopping condition is reached. Sometimes, however, you need to change that behavior.
Python provides three useful statements:
break— stops the loop completely.continue— skips the current iteration.pass— does nothing and allows the program to continue.
2. The break Statement
The break statement immediately terminates the loop.
for number in range(1, 10):
if number == 5:
break
print(number)The loop stops when number becomes 5.
CHECK YOUR UNDERSTANDING
What numbers are printed?
3. break with a while Loop
count = 1
while count <= 10:
if count == 4:
break
print(count)
count += 1The loop normally could reach 10, but break gives us an earlier stopping point.
CHECK YOUR UNDERSTANDING
When does this while loop stop?
4. break in Data Processing
In data processing, break can stop a search once the required record has been found.
customers = ["Amit", "Neha", "Ravi", "Priya"]
for customer in customers:
if customer == "Ravi":
print("Customer Found")
breakCHECK YOUR UNDERSTANDING
Why is break useful in this example?
5. The continue Statement
Unlike break, continue does not stop the entire loop. It skips the current iteration and moves to the next one.
for number in range(1, 6):
if number == 3:
continue
print(number)CHECK YOUR UNDERSTANDING
What is printed?
6. break vs continue
| Statement | What it does |
|---|---|
break | Stops the entire loop. |
continue | Skips the current iteration. |
CHECK YOUR UNDERSTANDING
Which statement skips only the current iteration?
7. continue in Data Analytics
Suppose an analyst wants to ignore missing values represented by zero.
sales = [10000, 0, 15000, 12000]
for amount in sales:
if amount == 0:
continue
print(amount)CHECK YOUR UNDERSTANDING
Which value is skipped?
8. The pass Statement
pass is a placeholder. It tells Python to do nothing at that point.
for number in range(3):
pass
print("Program finished")The loop executes, but pass performs no action.
CHECK YOUR UNDERSTANDING
What does pass do?
9. pass vs continue
These two statements behave differently.
for number in range(1, 4):
if number == 2:
pass
print(number)Because pass does nothing, the print statement still executes for 2.
CHECK YOUR UNDERSTANDING
Will 2 be printed in the previous example?
10. Predict the Output
for number in range(1, 6):
if number == 4:
continue
print(number)CHECK YOUR UNDERSTANDING
What is printed?
11. Complete the Code
Use ___ to stop a loop completely.
Use ___ to skip the current iteration.
Use ___ when you need a placeholder that does nothing.
12. Combining break with a Condition
sales = [25000, 40000, 65000, 120000, 90000]
for sale in sales:
if sale >= 100000:
print("Target Found")
breakThe loop stops when it encounters the first sale of at least ₹100,000.
CHECK YOUR UNDERSTANDING
Which sale causes the loop to stop?
13. Mini Challenge — Ignore Invalid Records
A dataset contains negative values that should not be processed.
values = [100, -20, 150, -5, 200]
for value in values:
if value < 0:
continue
print(value)CHECK YOUR UNDERSTANDING
How many values will be printed?
14. Mini Challenge — Find the First High Sale
Find the first transaction greater than ₹100,000 and stop processing after finding it.
sales = [45000, 70000, 125000, 150000]
for sale in sales:
if sale > 100000:
print(sale)
breakCHECK YOUR UNDERSTANDING
What value will be printed?
15. Debugging Loop Control
Look at this code:
for number in range(1, 6):
if number == 3:
break
print(number)
print("Finished")Once number becomes 3, the entire loop stops. Therefore, no code after the break inside that iteration runs.
CHECK YOUR UNDERSTANDING
Will 'Finished' be printed when number equals 3?
16. Final Challenge — Sales Quality Check
A data analyst wants to inspect sales records. Negative values should be ignored. If a sale reaches ₹200,000, processing should stop.
sales = [50000, -10000, 90000, 125000, 200000, 250000]
for sale in sales:
if sale < 0:
continue
if sale >= 200000:
print("Limit Reached")
break
print(sale)CHECK YOUR UNDERSTANDING
Which sales values are printed before 'Limit Reached'?
Lesson 15 Recap
- ✓
breakstops the entire loop. - ✓
continueskips the current iteration. - ✓
passdoes nothing and acts as a placeholder. - ✓ break is useful when a required item has been found.
- ✓ continue is useful for ignoring unwanted records.
- ✓ pass can be used when code is intentionally left empty.
- ✓ These statements can be combined with conditions.