Tutorials References Exercises Menu
Paid Courses

Go For Loops


The for loop loops through a block of code a specified number of times.


for Loops

Loops are handy if you want to run the same code over and over again, each time with a different value.

Each execution of a loop is called an iteration.

The for loop can take up to three statements:

Syntax

for statement1; statement2; statement3 {
   // code to be executed for each iteration
}

statement1 Initializes the loop counter value.

statement2 Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends..

statement3 Increases the loop counter value.

Note: These statements don't need to be present as loops arguments. However, they need to be present in the code in some form.


for Loop Examples

The following example will print the numbers from 0 to 4: 

Example

package main
import ("fmt")

func main() {
  for i:=0; i < 5; i++ {
    fmt.Println(i)
  }
}

Result:

0
1
2
3
4
Try it Yourself »

Example explained

  • i:=0; - Initialize the loop counter (i), and set the start value to 0
  • i < 5; - Continue the loop as long as i is less than 5
  • i++ - Increase the loop counter value by 1 for each iteration

The following example counts to 100 by tens: 

Example

package main
import ("fmt")

func main() {
  for i:=0; i <= 100; i+=10 {
    fmt.Println(i)
  }
}

Result:

0
10
20
30
40
50
60
70
80
90
100
Try it Yourself »

Example explained

  • i:=0; - Initialize the loop counter (i), and set the start value to 0
  • i <= 100; - Continue the loop as long as i is less than or equal to 100
  • i+=10 - Increase the loop counter value by 10 for each iteration

The continue Statement

The continue statement breaks one iteration in the loop - if a specified condition occurs, and then continues with the next iteration in the loop.

Example

This example skips the value of 3:

package main
import ("fmt")

func main() {
  for i:=0; i < 5; i++ {
    if i == 3 {
      continue
    }
    fmt.Println(i)
  }
}

Result:

0
1
2
4
Try it Yourself »

The break Statement

The break statement is used to terminate the loop execution.

Example

This example breaks out of the loop when i is equal to 3:

package main
import ("fmt")

func main() {
  for i:=0; i < 5; i++ {
    if i == 3 {
      break
    }
    fmt.Println(i)
  }
}

Result:

0
1
2
Try it Yourself »

Nested Loops

It is possible to place for loops inside other for loops.

Example

package main
import ("fmt")

func main() {
  var adj = [2]string{"big", "tasty"}
  var fruits = [3]string{"apple", "orange", "banana"}
  for i:=0; i < len(adj); i++ {
    for j:=0; j < len(fruits); j++ {
      fmt.Println(adj[i],fruits[j])
    }
  }
}

Result:

big apple
big orange
big banana
tasty apple
tasty orange
tasty banana
Try it Yourself »

Go Exercises

Test Yourself With Exercises

Exercise:

Print i as long as i is less than 6.

package main   
import ("fmt") 
func main() { i:=0; i < 6; { fmt.Println(i) } }

Start the Exercise