Introduction
An else-if ladder helps you check many conditions one by one and run code for the first true condition.
if condition1 { // code to run if condition1 is true } else if condition2 { // code to run if condition2 is true } else if condition3 { // code to run if condition3 is true } else { // code to run if none of the above conditions are true }
if score >= 90 { fmt.Println("Grade A") } else if score >= 75 { fmt.Println("Grade B") } else if score >= 60 { fmt.Println("Grade C") } else { fmt.Println("Fail") }
if age < 13 { fmt.Println("Child") } else if age < 20 { fmt.Println("Teenager") } else if age < 60 { fmt.Println("Adult") } else { fmt.Println("Senior") }
package main import "fmt" func main() { var temp int = 30 if temp > 40 { fmt.Println("It's very hot today.") } else if temp > 30 { fmt.Println("It's warm today.") } else if temp >= 20 { fmt.Println("It's nice today.") } else { fmt.Println("It's cold today.") } }
else-if ladder do in Go programming?else if as two separate words without parentheses around the condition.package main
import "fmt"
func main() {
x := 15
if x < 10 {
fmt.Println("Less than 10")
} else if x < 20 {
fmt.Println("Less than 20")
} else {
fmt.Println("20 or more")
}
}if score >= 90 {
fmt.Println("A")
} else if score >= 80
fmt.Println("B")
} else {
fmt.Println("C")
}age < 13.age >= 13 && age <= 19.Pass the test and this pattern moves unknown