0
0
Pythonprogramming~15 mins

Break vs continue execution difference in Python - Hands-On Comparison

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use continue to skip cold days. Increase count for warm days. Use print() to show results.