Break Statement Behavior
📖 Scenario: Imagine you are checking a list of daily temperatures to find the first day when the temperature reaches or exceeds a certain limit. Once you find that day, you want to stop checking further days.
🎯 Goal: You will write a program that uses a break statement inside a for loop to stop the loop early when a condition is met.
📋 What You'll Learn
Create a list called
temperatures with the exact values: [22, 25, 19, 30, 28, 35, 24]Create a variable called
limit and set it to 30Use a
for loop with the variable temp to go through temperaturesInside the loop, use an
if statement to check if temp is greater than or equal to limitUse
break to stop the loop when the condition is trueAfter the loop, print the text
"First temperature reaching the limit or above: " followed by the value of temp💡 Why This Matters
🌍 Real World
Checking sensor data or measurements to stop processing once a certain threshold is reached is common in many real-world applications like weather monitoring or quality control.
💼 Career
Understanding how to control loops with break statements is important for writing efficient programs that do not waste time checking unnecessary data.
Progress0 / 4 steps