Python The Unified For Loop & Range Iteration
Go simplifies iteration by having only one loop keyword: for. It unifies 4 patterns: traditional 3-component index loops, condition-only while loops, infinite loops (for {}), and range loops over slices, maps, strings, and channels.
"The Go for loop is a Swiss Army knife that replaces while, do-while, and foreach with a single versatile blade."
Deep Dive: How It Works
3-Component Loop: for i := 0; i < n; i++ { ... }
While Equivalent: for condition { ... }
Range Iteration: for index, value := range slice { ... }
Syntax Blueprint
for i := 0; i < 5; i++ {
fmt.Println(i)
}
for idx, item := range items {
fmt.Printf("%d: %s\n", idx, item)
}Index iteration and collection range traversal using the for keyword.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Taking the memory address of the range iteration value pointer: &val.Why it happens: In older Go versions, val was a single shared variable reused across iterations.
How to fix: Index the slice directly: &items[i].
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Calculate Sum of Squares with For Loop
Calculate the sum of squares for integers 1 through 4 (1^2 + 2^2 + 3^2 + 4^2 = 30).
Print "Sum of Squares: %d\n" with total sum.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.