Skip to main content

Introduction

for is Go’s only looping construct. Unlike other languages, Go doesn’t have while or do-while loops - the for loop handles all iteration needs through various forms.

The Code

// `for` is Go's only looping construct. Here are
// some basic types of `for` loops.

package main

import "fmt"

func main() {

	// The most basic type, with a single condition.
	i := 1
	for i <= 3 {
		fmt.Println(i)
		i = i + 1
	}

	// A classic initial/condition/after `for` loop.
	for j := 0; j < 3; j++ {
		fmt.Println(j)
	}

	// Another way of accomplishing the basic "do this
	// N times" iteration is `range` over an integer.
	for i := range 3 {
		fmt.Println("range", i)
	}

	// `for` without a condition will loop repeatedly
	// until you `break` out of the loop or `return` from
	// the enclosing function.
	for {
		fmt.Println("loop")
		break
	}

	// You can also `continue` to the next iteration of
	// the loop.
	for n := range 6 {
		if n%2 == 0 {
			continue
		}
		fmt.Println(n)
	}
}

Loop Variants

While-Style Loop (Condition Only)

i := 1
for i <= 3 {
	fmt.Println(i)
	i = i + 1
}
This form acts like a while loop in other languages. It continues as long as the condition is true.

Classic Three-Component Loop

for j := 0; j < 3; j++ {
	fmt.Println(j)
}
The traditional for loop with:
  1. Initialization: j := 0
  2. Condition: j < 3
  3. Post statement: j++
This is the most common form when you need to iterate a specific number of times with an index.

Range Over Integer

for i := range 3 {
	fmt.Println("range", i)
}
A modern Go feature that iterates from 0 up to (but not including) the number. This is cleaner than for i := 0; i < 3; i++ when you just need the index.
The range over integer syntax is available in Go 1.22 and later.

Infinite Loop

for {
	fmt.Println("loop")
	break
}
A for loop without any condition runs forever until explicitly stopped with break or return.
Infinite loops are useful for servers, event loops, and other continuously running processes.

Using Continue

for n := range 6 {
	if n%2 == 0 {
		continue
	}
	fmt.Println(n)
}
continue skips to the next iteration of the loop. This example prints only odd numbers (1, 3, 5).

Loop Control Statements

StatementEffect
breakExit the loop entirely
continueSkip to next iteration
returnExit the enclosing function

Common For Loop Patterns

Iterate N Times

for i := 0; i < n; i++ {
    // do something
}

Iterate Until Condition

for condition {
    // do something
}

Iterate Forever

for {
    // do something
    if done {
        break
    }
}

Iterate with Range

for i := range n {
    // i goes from 0 to n-1
}
Go’s for loop doesn’t require parentheses around the condition, but the braces {} are always required.

Key Takeaways

  • for is the only loop in Go - it replaces while and do-while
  • Three forms: condition-only, three-component, and infinite
  • range over integers provides a clean way to iterate N times
  • Use break to exit loops and continue to skip iterations
  • No parentheses needed around conditions, but braces are required
  • Loop variables declared in the initialization are scoped to the loop

Build docs developers (and LLMs) love