Introduction
An if-else statement helps your program make choices. It runs different code depending on a condition.
An if-else statement helps your program make choices. It runs different code depending on a condition.
if condition { // code to run if condition is true } else { // code to run if condition is false }
The condition must be a statement that results in true or false.
Curly braces { } are required even for one line of code inside if or else.
if age >= 18 { fmt.Println("You are an adult.") } else { fmt.Println("You are a minor.") }
if score > 50 { fmt.Println("Passed") } else { fmt.Println("Failed") }
This program checks the temperature and prints if it is hot or cool.
package main import "fmt" func main() { temperature := 30 if temperature > 25 { fmt.Println("It's hot outside.") } else { fmt.Println("It's cool outside.") } }
You can add more conditions using else if for multiple choices.
Always use curly braces to avoid mistakes, even if the block has one line.
If-else lets your program choose between two paths.
Use it when you want to run code only if a condition is true or false.
Remember to write conditions that result in true or false.
if-else statement do in Go?package main
import "fmt"
func main() {
x := 5
if x > 10 {
fmt.Println("Greater")
} else {
fmt.Println("Smaller or equal")
}
}package main
import "fmt"
func main() {
x := 7
if x > 5
fmt.Println("Big")
else
fmt.Println("Small")
}Pass the test and this pattern moves unknown