Go If and Switch
ExampleGo
package main
import "fmt"
func main() {
score := 72
if score >= 90 {
fmt.Println("A")
} else if score >= 70 {
fmt.Println("B")
} else {
fmt.Println("C")
}
if n := score * 2; n > 100 { // init statement
fmt.Println("double is big:", n)
}
switch day := 3; day {
case 1:
fmt.Println("Monday")
case 2, 3:
fmt.Println("Tuesday or Wednesday")
default:
fmt.Println("Other")
}
}Output
B double is big: 144 Tuesday or Wednesday
Go conditions have no brackets but the braces are required. switch does not fall through: there is no break needed.
Quick check
Does a Go switch case need break?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.