If, Else If and Nested If
if runs code when a condition is true. else handles the other case and else if adds more checks. An if can start with a short statement, such as if x := f(); x > 0. Conditions never need parentheses.
Example
score := 72
if score >= 85 {
fmt.Println("Distinction")
} else if score >= 70 {
fmt.Println("Pass")
} else {
fmt.Println("Retake")
}
if n := score / 10; n >= 7 {
fmt.Println("tens digit:", n)
}
age, member := 20, true
if age >= 18 {
if member {
fmt.Println("member adult")
}
}Result
Pass tens digit: 7 member adult
Exercise
Add a second check.
if a {} if b {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.