if and else
No parentheses, braces required. An if can have a short statement first.
Example
package main
import "fmt"
func main() {
n := 15
if n%15 == 0 {
fmt.Println("FizzBuzz")
} else if n%3 == 0 {
fmt.Println("Fizz")
} else {
fmt.Println(n)
}
if v := n * 2; v > 20 {
fmt.Println(v)
}
}Result
FizzBuzz 30
Exercise
Else branch.
if x {} {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.