Go · Stage 4 – Repeating Things · Lesson 25 of 64

A Counting for Loop

Repeat a block a known number of times with a counter.

Go code

for i := 1; i <= 3; i++ {
    fmt.Println(i)
}
Line-by-line explanation
  1. i := 1 creates the counter once.
  2. i <= 3 is checked before each iteration.
  3. i++ adds one after each iteration.
  4. Println displays the current count.
Expected output
1
2
3

This is the expected result.