Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Break vs Continue Execution Difference
📖 Scenario: Imagine you are checking a list of daily temperatures to find if there is a very hot day or to skip cold days while counting warm days.
🎯 Goal: You will write Python code to see how break and continue change the flow inside a loop.
📋 What You'll Learn
Create a list called temperatures with exact values: 22, 25, 30, 35, 28, 40, 20
Create a variable called hot_day_found and set it to False
Use a for loop with variable temp to go through temperatures
Inside the loop, use break to stop when temperature is 35 or more
Create a variable called warm_days_count and set it to 0
Use a for loop with variable temp to go through temperatures
Inside the loop, use continue to skip temperatures less than 25
Increase warm_days_count by 1 for each warm day
Print the values of hot_day_found and warm_days_count
💡 Why This Matters
🌍 Real World
In real life, you might want to stop checking data once you find a critical value or skip irrelevant data points.
💼 Career
Understanding <code>break</code> and <code>continue</code> helps in writing efficient loops in data processing, automation, and many programming tasks.
Progress0 / 4 steps
1
Create the temperature list
Create a list called temperatures with these exact values: 22, 25, 30, 35, 28, 40, 20
Python
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Set up a variable to find a hot day
Create a variable called hot_day_found and set it to False
Python
Hint
Use = to assign False to the variable.
3
Use break to stop when a hot day is found
Use a for loop with variable temp to go through temperatures. Inside the loop, if temp is 35 or more, set hot_day_found to True and use break to stop the loop.
Python
Hint
Use for temp in temperatures: to loop. Use if to check temperature. Use break to stop the loop.
4
Count warm days using continue and print results
Create a variable called warm_days_count and set it to 0. Use a for loop with variable temp to go through temperatures. Inside the loop, use continue to skip temperatures less than 25. Increase warm_days_count by 1 for each warm day. Finally, print hot_day_found and warm_days_count.
Python
Hint
Use continue to skip cold days. Increase count for warm days. Use print() to show results.
Practice
(1/5)
1. What does the break statement do inside a loop in Python?
easy
A. Stops the entire loop immediately
B. Skips the current iteration and continues with the next
C. Ends the current function
D. Restarts the loop from the beginning
Solution
Step 1: Understand the role of break
The break statement is used to exit a loop completely when a condition is met.
Step 2: Compare with other options
Unlike continue, which skips one iteration, break stops the loop entirely.
5. You want to print all numbers from 0 to 9 except 5, but stop printing if the number reaches 8. Which code snippet correctly uses break and continue?
hard
A. for i in range(10):
if i == 5:
break
if i == 8:
continue
print(i)
B. for i in range(10):
if i == 5:
continue
if i == 8:
break
print(i)
C. for i in range(10):
if i == 8:
continue
if i == 5:
break
print(i)
D. for i in range(10):
if i == 5:
continue
if i == 8:
continue
print(i)
Solution
Step 1: Understand requirements for skipping and stopping
We skip printing 5 (use continue) and stop printing at 8 (use break).
Step 2: Check each option for correct order and logic
for i in range(10):
if i == 5:
continue
if i == 8:
break
print(i) correctly continues at 5 to skip it, breaks at 8 to stop loop, and prints others.
Final Answer:
Option B code snippet -> Option B
Quick Check:
continue skips 5, break stops at 8 [OK]
Hint: Continue skips unwanted, break stops loop early [OK]