100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

for Loops in Go

Master Go's single looping construct, which covers classic for, while-style, infinite, and range-based loops.

Control FlowBeginner9 min readJul 8, 2026
Analogies

Introduction

Go simplifies looping by providing only a single keyword, for, which covers every looping pattern found in other languages, including while and do-while style iteration. This design choice reduces the mental overhead of choosing between multiple loop constructs and keeps the language's syntax minimal, while still supporting classic C-style loops, condition-only loops, infinite loops, and range-based iteration over collections.

🏏

Cricket analogy: A coach who drills one basic batting stance for every situation - defense, attack, rotating strike - instead of teaching five different stances mirrors Go's single for keyword covering every loop pattern.

Syntax

go
// classic three-part loop
for i := 0; i < 10; i++ {
    // body
}

// while-style loop
for condition {
    // body
}

// infinite loop
for {
    // body, use break to exit
}

// range-based loop
for index, value := range collection {
    // body
}

Explanation

The classic three-part for loop has an init statement, a condition, and a post statement, all optional and separated by semicolons. Omitting the init and post parts while keeping only the condition produces while-style behavior. Omitting everything, including the semicolons, produces an infinite loop that must be exited with break or a return. The range keyword iterates over arrays, slices, strings, maps, and channels, yielding an index or key plus the corresponding value; for maps the iteration order is randomized by design, and for channels range yields values until the channel is closed.

🏏

Cricket analogy: Setting a bowler's field with an initial placement, a condition to bowl until 6 overs are done, and a post-over adjustment mirrors the three-part for loop; dropping init and post for just 'while wickets remain' gives while-style overs, and range over the batting lineup mirrors listing each batsman with their score.

Example

go
package main

import "fmt"

func main() {
    // classic loop
    sum := 0
    for i := 1; i <= 5; i++ {
        sum += i
    }
    fmt.Println("sum:", sum)

    // while-style loop
    n := 8
    for n > 1 {
        if n%2 == 0 {
            n /= 2
        } else {
            n = 3*n + 1
        }
    }
    fmt.Println("final n:", n)

    // range over a slice
    fruits := []string{"apple", "banana", "cherry"}
    for i, f := range fruits {
        fmt.Println(i, f)
    }
}

Output

go
sum: 15
final n: 1
0 apple
1 banana
2 cherry

Key Takeaways

  • Go has only one looping keyword: for.
  • Omitting init and post creates a while-style loop.
  • Omitting everything creates an infinite loop, exited with break.
  • range yields index/key plus value for slices, arrays, strings, maps, and channels.
  • Map iteration order via range is intentionally randomized each run.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#ForLoopsInGo#Loops#Syntax#Explanation#Example#StudyNotes#SkillVeris