×

go · level 1 · unit 5 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A repeats a block for you.

A loop repeats lines for you. Picture doing star jumps: you count 1, 2, 3, jumping each time. A for loop counts the same way, running its block once for every number.

for i := 1; i <= 3; i++ { has three parts, split by semicolons. Start: make a box i holding 1. Check: keep going while i <= 3. Step: after each round, i++ adds one to i.

Each time round, i holds the current count, so you can use it inside the block.

package main
import "fmt"
func main() {
	for i := 1; i <= 3; i++ {
		fmt.Println("Star jump number", i)
	}
	fmt.Println("Phew!")
}

Programmers often start counting at 0, so for i := 0; i < 3; i++ also runs 3 times.