Golang for Loop

Last Updated: September 13, 2022

Syntax of Golang for loop

 for (init; condition; post) { 
  statements
 }

init statement: execute before the first iteration starts

condition expression: evaluate before the iteration starts

post statement: evaluate at the end of the iteration

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

There are several ways to write for loop in GO

max := 100
a := 0
for a <= max {
  println(a)
  a++
}

Infinite Loop

for { 
   
}
Test