Go For Loop
The Go for statement is used for repeating a set of statements number of times. It is the only loop in go language.
There are two variants of for loop in Go: Counter-controlled iteration and Condition-controlled iteration.
When the execution of the loop is over, the objects created inside the loop gets destroyed.
Go For Loop counter-controlled iteration example:
0 1 2 3 4 5 6 7 8 9 10
As you can see in the above example, loop begins with the initialization stage, variable for i (i:= 0); This is done only once. It is followed by a conditional check i (i < 10). Conditional check is performed in every iteration. The for loop stops when the condition becomes false.
Go Nested For Loop Example:
0 3 0 2 0 1 1 3 1 2 1 1 2 3 2 2 2 1
Go Infinitive For Loop
In infinite for loop, the conditional statement is absent like:
Go Infinitive For Loop Example:
This loop will run forever. This loop will run forever. This loop will run forever. This loop will run forever. This loop will run forever. This loop will run forever. This loop will run forever.
Go For - Condition-controlled iteration
The for loop which has no header is used for condition-controlled iteration. It is similar to while-loop in other languages.
For Loop Example in while fashion:
2 4 8 16 32 64 128