0
0
Goprogramming~15 mins

Break statement in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Break Statement in Go
๐Ÿ“– 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 Go program that uses a break statement inside a for loop to stop the loop when the temperature limit is reached.
๐Ÿ“‹ What You'll Learn
Create a slice of integers called temperatures with the exact values: 23, 25, 28, 30, 32, 29, 27
Create an integer variable called limit and set it to 30
Use a for loop with the variable temp to iterate over temperatures
Inside the loop, use an if statement to check if temp is greater than or equal to limit
Use the break statement to exit the loop when the condition is true
Print the message "Temperature limit reached: X" where X is the temperature that caused the break
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Checking sensor data or daily measurements often requires stopping early when a threshold is reached to save time and resources.
๐Ÿ’ผ Career
Understanding how to control loops with break statements is important for writing efficient programs in many software development and data processing jobs.
Progress0 / 4 steps
1
Create the temperature data
Create a slice of integers called temperatures with these exact values: 23, 25, 28, 30, 32, 29, 27
Go
Need a hint?

Use temperatures := []int{...} to create the slice with the exact numbers.

2
Set the temperature limit
Create an integer variable called limit and set it to 30
Go
Need a hint?

Use limit := 30 to create the variable.

3
Use a for loop with break
Use a for loop with the variable temp to iterate over temperatures. Inside the loop, use an if statement to check if temp is greater than or equal to limit. Use the break statement to exit the loop when the condition is true.
Go
Need a hint?

Use for _, temp := range temperatures to loop. Inside, check if temp >= limit and then break.

4
Print the temperature that caused the break
Print the message "Temperature limit reached: X" where X is the temperature that caused the break. Use fmt.Println to display the message.
Go
Need a hint?

Use fmt.Println("Temperature limit reached:", temp) before the break.