If, For and Switch
if needs no parentheses but requires braces. for is the only loop and covers while style and range. switch needs no break and stops after the first match.
Example
for i := 1; i <= 3; i++ {
fmt.Println("i =", i)
}
n := 3
for n > 0 {
n--
}
score := 72
if score >= 70 {
fmt.Println("Pass")
}
day := 2
switch day {
case 1:
fmt.Println("Mon")
case 2:
fmt.Println("Tue")
default:
fmt.Println("Other")
}Result
i = 1 i = 2 i = 3 Pass Tue
Exercise
Which keyword makes a loop?
i := 0; i < 3; i++ {}Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.