Back to Course
Control Flow

Understanding Control Flow: If-Else, Switch, and Loops in Go

if-else in Go

age := 20
if age >= 18 {
    fmt.Println("Adult")
} else {
    fmt.Println("Minor")
}

switch Statement

day := 3
switch day {
case 1:
    fmt.Println("Monday")
case 2:
    fmt.Println("Tuesday")
default:
    fmt.Println("Other day")
}

Unlike C or Java, Go's switch doesn't require a break — it doesn't fall through to the next case by default.

Loops in Go

Go has only one looping construct: for, which can be used in multiple ways.

for i := 0; i < 5; i++ {
    fmt.Println(i)
}

// while-style loop
i := 0
for i < 5 {
    fmt.Println(i)
    i++
}

// infinite loop
for {
    break
}

Ready to master real-world Go skills?

Learn Go hands-on with mentor-led, live sessions.

Explore Course