Control Flow

For Loops

Go only has

for
loops (no while, do-while).

Basic For

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

While-style

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

Infinite Loop

for {
    // Runs forever
    // Use break to exit
}

Range (iterate)

nums := []int{1, 2, 3}
for index, value := range nums {
    fmt.Println(index, value)
}
Visualizer
for loop iteration
1
2
3
4
5
✓ Loop completed

Interactive Visualization

go
Loading...
Output
Run execution to see output...