Quidest?

Init and Post statements in For loops in Go

ยท Lorenzo Drumond

Go has only one looping construct, the for loop.

The basic for loop has three components separated by semicolons:

the init statement: executed before the first iteration the condition expression: evaluated before every iteration the post statement: executed at the end of every iteration

The init statement will often be a short variable declaration, and the variables declared there are visible only in the scope of the for statement.

The loop will stop iterating once the boolean condition evaluates to false.

1for x:= 0; x < 10; x++ {
2  fmt.Println(x)
3}

The init and post statement are optional

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	sum := 1
 7	for sum < 1000 {
 8		sum += sum
 9	}
10	fmt.Println(sum)
11}

References

Next -> continue-and-break-in-for-loops-in-go

#programming #range #statement #post #infinite #golang #repeat #for #loops #init #assignments