0
0
Javaprogramming~15 mins

Break statement in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Break Statement in Java Loops
📖 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 Java program that uses a break statement inside a for loop to stop the loop early when a temperature threshold is met.
📋 What You'll Learn
Create an array of temperatures with exact values
Create an integer variable for the temperature threshold
Use a for loop to check each temperature with variables i and temp
Use a break statement to exit the loop when the temperature reaches or exceeds the threshold
Print the index of the first day that meets the threshold
💡 Why This Matters
🌍 Real World
Checking sensor data or daily measurements to find the first occurrence of a condition is common in weather apps, health monitors, and quality control.
💼 Career
Understanding how to use break statements helps developers write efficient loops that stop processing when no longer needed, saving time and resources.
Progress0 / 4 steps
1
Create the temperature array
Create an integer array called temperatures with these exact values: 23, 25, 28, 30, 27, 31, 29.
Java
Need a hint?

Use curly braces {} to list the values inside the array.

2
Set the temperature threshold
Create an integer variable called threshold and set it to 30.
Java
Need a hint?

Use int threshold = 30; to create the variable.

3
Use a for loop with break
Write a for loop using int i = 0; i < temperatures.length; i++ and inside the loop assign int temp = temperatures[i]. Use an if statement to check if temp >= threshold. If true, use break to exit the loop.
Java
Need a hint?

Remember to declare int i before the loop so you can use it after the loop ends.

4
Print the first day meeting the threshold
Write a System.out.println statement to print: "First day with temperature >= 30 is: " followed by the value of i.
Java
Need a hint?

The first temperature >= 30 is at index 3, so the output should show that.