The for Loop: All Patterns
Classic, while-style, infinite, and range loops
The for Loop: All Patterns is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Go Has Only One Loop
Go has only the for loop — no while, no do-while. The for keyword covers all iteration patterns through different forms.
Classic C-Style for Loop
The traditional three-clause loop:
for i := 0; i < 5; i++ {
fmt.Println(i)
}While-Style Loop
Omit the init and post clauses for a while-like loop:
n := 1
for n < 100 {
n *= 2
}Infinite Loop
Omit all clauses for an infinite loop (must use break or return to exit):
for {
msg := receive()
if msg == "quit" {
break
}
process(msg)
}range over Slice
range iterates over a slice, returning index and value:
fruits := []string{"apple", "banana", "cherry"}
for i, fruit := range fruits {
fmt.Printf("%d: %s\n", i, fruit)
}range over Map
range over a map returns key-value pairs (iteration order is random):
scores := map[string]int{"Alice": 90, "Bob": 85}
for name, score := range scores {
fmt.Printf("%s: %d\n", name, score)
}range over String
Ranging over a string gives you rune values (Unicode code points), not bytes:
for i, r := range "Go!" {
fmt.Printf("index %d: %c\n", i, r)
}range over Channel
Range over a channel reads values until the channel is closed:
ch := make(chan int, 3)
ch <- 1; ch <- 2; ch <- 3
close(ch)
for v := range ch {
fmt.Println(v)
}break and continue
break exits the innermost loop; continue skips to the next iteration:
for i := 0; i < 10; i++ {
if i%2 == 0 {
continue // skip even numbers
}
if i > 7 {
break // stop at 7
}
fmt.Println(i)
}Labeled break
Labels let you break out of an outer loop from inside an inner loop:
outer:
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if i+j == 3 {
break outer
}
}
}Quick Check
How do you write an infinite loop in Go?
Recap: The for Loop
Go's for loop covers all iteration patterns:
- Classic:
for i := 0; i < n; i++ - While-style:
for condition - Infinite:
for {} - Range: over slices, maps, strings, channels
break/continuewith optional labels for nested loops
Next: switch statements in Go.
Frequently asked questions
Is the “The for Loop: All Patterns” lesson free?
Yes — the full text of “The for Loop: All Patterns” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “The for Loop: All Patterns”?
Classic, while-style, infinite, and range loops You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The for Loop: All Patterns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if Statements and Init Expressions
- The for Loop: All Patterns
- switch Statements in Go
- break, continue and Labels