In Golang what does it means that the pre and post statements of a for loop are empty like in this example:
sum := 1
for ; sum < 10; {
sum += sum
}
fmt.Println(sum)
In Golang what does it means that the pre and post statements of a for loop are empty like in this example:
sum := 1
for ; sum < 10; {
sum += sum
}
fmt.Println(sum)
Remember that a for loop is the same as a while loop. Your code can be rewritten in other languages as
sum := 1
while(sum < 10) {
sum += sum
}
fmt.Println(sum)
In a for
loop, there are 3 parts.
for(initial statement ; condition ; end statement usually iterate)
This is equivalent to
initial statement
while(condition) {
Stuff here
End iteration statement
}
The reason your loop can be written withiut the pre and post statements is because you've specified them in other parts of the code.