OneBite.Dev - Coding blog in a bite size

implement a three nested loop in Go

Code snippet on how to implement a three nested loop in Go

  package main

import "fmt"

func main() {
  i := 0
  j := 0
  k := 0

  for i = 0; i < 3; i++ {
    for j = 0; j < 3; j++ {
      for k = 0; k < 3; k++ {
        fmt.Printf("i=%d, j=%d, k=%d\n", i, j, k)
      }
    }
  }
}

This code uses three nested loops to print out all the combinations of three numbers between 0 and 2. The three integers start at 0 and increment by 1 on each loop until they hit 3. The first loop increments the first number (i), then the second loop increments the second number (j), the third loop increments the third number (k), and each combination of the three is printed out. The outer loop (i) is the most outer loop and will loop the most times compared to the other two. When the outer loop is completed, the nested loops will also be completed and the code will exit.

go