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 to save time.
🎯 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 an array called
temperatures with the exact values: 23, 25, 28, 30, 27, 31, 29Create an integer variable called
threshold and set it to 30Use a
for loop with the variable i to iterate over temperaturesInside the loop, use an
if statement to check if temperatures[i] is greater than or equal to thresholdUse the
break statement to exit the loop when the condition is trueAfter the loop, print the index
i where the temperature first reached or exceeded the threshold💡 Why This Matters
🌍 Real World
Checking sensor data or daily measurements to find the first occurrence of a critical value quickly.
💼 Career
Understanding how to stop loops early is important for writing efficient programs in many software development jobs.
Progress0 / 4 steps