WeCodeAcademy

Go Loops

Go has just one loop keyword: for. It can act like a for, a while, or loop forever.

ExampleGo
package main

import "fmt"

func main() {
  for i := 1; i <= 3; i++ {
    fmt.Println("for", i)
  }

  n := 3
  for n > 0 { // like while
    fmt.Println("while", n)
    n--
  }

  for i := 0; i < 10; i++ {
    if i == 2 {
      continue
    }
    if i == 4 {
      break
    }
    fmt.Println("i =", i)
  }

  for i, c := range "Go!" {
    fmt.Println(i, string(c))
  }
}
Output
for 1
for 2
for 3
while 3
while 2
while 1
i = 0
i = 1
i = 3
0 G
1 o
2 !
Quick check

How many loop keywords does Go have?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.