Loops

Course 1 · Ch 4
Loops: for as Go's Only Loop Keyword
One keyword, four shapes — covering everything JavaScript split across for, while, for...of, and for...in

JavaScript has four loop keywords: for, while, for...of, for...in. Go has exactly onefor — and reshapes it to cover every one of those cases. Recognising which "shape" of for is in use is the main new skill this chapter teaches.

Shape 1: The Classic Counting Loop

for i := 0; i < 5; i++ { fmt.Println(i) } // 0 1 2 3 4

This looks almost identical to JavaScript's for (let i = 0; i < 5; i++) — minus the parentheses and semicolons-as-separators staying the same. i++ works exactly as before; Go also has i--, but notably no ++i prefix form — increment/decrement are statements in Go, not expressions, so they're always written after the variable.

Shape 2: for as a while Loop

count := 0 for count < 3 { fmt.Println("Still counting:", count) count++ } // Still counting: 0 // Still counting: 1 // Still counting: 2

Dropping the init and post clauses, leaving only a condition, turns for into the exact equivalent of JavaScript's while — there is no separate while keyword in Go at all, this is simply how it's written.

Shape 3: An Infinite Loop, with break

attempts := 0 for { attempts++ fmt.Println("Attempt", attempts) if attempts == 3 { break } }

for with absolutely nothing after it loops forever, until something inside explicitly breaks out — the Go equivalent of JavaScript's while (true) { ... break; }. continue also works exactly as it does in JavaScript, skipping straight to the next iteration.

Shape 4: for...range — Iterating a Slice

fruits := []string{"apple", "banana", "cherry"} for index, fruit := range fruits { fmt.Println(index, fruit) } // 0 apple // 1 banana // 2 cherry

[]string{...} is a slice — Go's closest equivalent to a JavaScript array, covered properly next chapter. for index, value := range fruits is the direct equivalent of JavaScript's for...of, except it always hands back both the index AND the value together — there's no separate index-only loop needed.

Ignoring the index with the blank identifier _
If only the value is needed, the index can be discarded with Go's "blank identifier": for _, fruit := range fruits { ... }. Go requires every declared variable to be used (Chapter 1), so _ exists specifically to say "I know this value exists, I'm deliberately not using it."

Looping Over a Map (Go's Object Equivalent)

scores := map[string]int{"maths": 88, "art": 95} for subject, score := range scores { fmt.Println(subject, score) }

The same range keyword also walks over a map (covered fully in Chapter 7) — Go's rough equivalent of a JavaScript object — handing back each key and value pair, the conceptual cousin of JavaScript's for...in.

Map iteration order is NOT guaranteed
Unlike a slice, where range always visits elements 0, 1, 2... in order, ranging over a map visits entries in a deliberately randomised order every time the program runs. Code that depends on a specific order from a map will behave unpredictably — sort the keys first if order matters.
JavaScriptGo
for (let i = 0; i < n; i++)for i := 0; i < n; i++
while (cond)for cond
while (true) { ... break; }for { ... break }
for (const item of array)for _, item := range slice
for (const key in object)for key, value := range map

Coding Challenges

Challenge 1

Write a classic counting for loop that prints every even number from 0 to 10 (inclusive), using i += 2 in the post clause instead of i++.

📄 View solution
Challenge 2

Write a for loop used as a while loop: starting with balance := 100, keep subtracting 30 and printing the new balance each time, stopping (using the condition, not break) once balance would go below 0.

📄 View solution
Challenge 3

Given colours := []string{"red", "green", "blue", "yellow"}, use for...range with the blank identifier _ to print just the values, one per line, with no index shown.

📄 View solution

Chapter 4 Quick Reference

  • for i := 0; i < n; i++ { } — classic counting loop, like JavaScript's for
  • for condition { } — Go's while loop; there is no separate while keyword
  • for { } — infinite loop; use break to exit, continue to skip an iteration
  • for index, value := range slice { } — like for...of, but always gives both index and value
  • for key, value := range map { } — like for...in, conceptually; order is NOT guaranteed
  • _ (blank identifier) — discards a value Go would otherwise require you to use
  • No ++i prefix form — i++ and i-- are statements, always written after the variable
  • Next chapter: functions — multiple return values, named returns, and variadic parameters