Welcome! Today, we're diving into conditional statements in Go, focusing on the if
statement, and we'll explore how to utilize the break
and continue
statements within loops. As you've learned, loops execute code multiple times, but by integrating conditional logic and these control flow statements, we can create more powerful and efficient programs. Let's explore these concepts in Go!
In Go, the if
statement allows us to execute actions based on certain conditions. Take a look at this example that determines what message to print based on the temperature
value:
Go1package main 2 3import "fmt" 4 5func main() { 6 temperature := 15 7 8 if temperature > 20 { 9 fmt.Println("Wear light clothes.") // This message will print if the temperature is over 20. 10 } else { 11 fmt.Println("Bring a jacket.") // This message will print otherwise. 12 } 13}
We can also evaluate multiple conditions using else if
. This means, "If the earlier condition isn't true, then check this one":
Go1package main 2 3import "fmt" 4 5func main() { 6 temperature := 15 7 8 if temperature > 30 { 9 fmt.Println("It's hot outside!") // This will print if the temperature is over 30. 10 } else if temperature > 20 { 11 fmt.Println("The weather is nice.") // This will print if the temperature is between 21 and 30. 12 } else { 13 fmt.Println("It might be cold outside.") // This will print if the temperature is 20 or below. 14 } 15}
The break
statement in Go is used to exit a loop early when a specific condition is met:
Go1package main 2 3import "fmt" 4 5func main() { 6 numbers := []int{1, 3, 7, 9, 12, 15} 7 8 for _, number := range numbers { 9 if number%2 == 0 { 10 fmt.Println("The first even number is:", number) // This prints the first even number. 11 break // This stops the loop after printing the first even number. 12 } 13 fmt.Println("Number:", number) 14 } 15}
The continue
statement allows us to skip the rest of the loop code for the current iteration only:
Go1package main 2 3import "fmt" 4 5func main() { 6 for i := 0; i < 6; i++ { 7 if i == 3 { 8 continue // This skips the print command for '3'. 9 } 10 fmt.Println(i) // This prints the numbers from 0 to 5 except 3. 11 } 12}
By using the tools we've discussed, we can develop more flexible loops. Here's a snippet where we stop the loop once we find "Charlie":
Go1package main 2 3import "fmt" 4 5func main() { 6 names := []string{"Alice", "Bob", "Charlie", "David"} 7 8 for _, name := range names { 9 if name == "Charlie" { 10 fmt.Println("Found Charlie!") // This prints when 'Charlie' is found. 11 break // This stops the loop after finding 'Charlie'. 12 } 13 } 14}
Congratulations! You've now learned about Go's if
statement, as well as the break
and continue
statements and their applications within loops. To solidify your understanding, you are encouraged to complete the following practice exercises: write a program that uses if
statements to decide what flavor of ice cream to serve based on a given temperature range, implement a loop that skips printing numbers divisible by a given value using continue
, and finally, create a loop that breaks when a condition is met, such as finding a specific word in a list. Happy coding!