6. For Loops — A Book of Go
May 15, 2024
For is the only loop in Go.
It consists of parts as usual in programming languages:
- init statement
- condition expression
- post statement
- But no ( ) surrounding the statements
- The init and post statements are optional:
sum := 1
for ; sum < 1000; {
sum += sum
}
Using for as while
Instead of using `while`one can use `for`almost in the same way:
for sum < 1000 {
}
Infinite loops
for {
}
Next Chapter: Arrays and slices