For Loop in Golang
Overview
Loop in programming is used for repeating the block of code for a finite or infinite duration of time. In this article, we will be learning all about for loop in Golang with an example.
What is for loop?
For loop in Golang is used for repeating a block of code until a condition specified in the loop is met. For is the only loop available in Golang to execute a block of code repeatedly. In this, all three parameters are optional.
Flow Control
The flow control of the for loop is as follows:
- If a condition is mentioned in the for loop then as long as the condition is true, the loop executes.
- A loop works on 3 parameters, initial state, condition, and a state change operation. If init; condition; the post is present in a for loop, then:
- The first step is initializing the state, it executes only once. In this step, the initialization of any loop variable is done.
- The next step is to evaluate the condition, the loop body is executed till the condition is true. If the condition i is false then the loop does not execute, instead, the control goes to the next statement after the for a loop.
- Once the body of the loop is executed or as a state change, the flow control goes back to the post-stated. The posted statement includes control variables that need to be updated.
- The condition is however evaluated once again and if it's true, the loop is executed and the whole process is repeated. The loop terminates when the condition becomes false.
Note: All the 3 parameters mentioned in the for loop are optional.
Syntax
The syntax is:
Since these three are optional, if we skip init and post we can write: like while loop
If we skip the condition we can write: like an infinite loop
Flow Diagram
Example 1: for
Program to print the first 10 natural numbers
Output:
Explanation:
In this example, a for loop is initialized with the value of i starting from i all way to 10. At each iteration, the value of i is printed. Once the value of i becomes 11, the condition becomes false and the control goes out of the loop printing the value of i from 1 to 10.
Example 2: Use for as while loop
Program to print numbers between 1 and 10
Output:
Explanation:
Here in this example, the value of n is initialized as 1. The for loop here act as a while loop and the loop is executed until the value of n is less than equal to 10. The statement executed is printing a variable n and incrementing its value. Once the value of n becomes greater than 10, the loop terminates and the control goes out of the loop.
Example 3: Use range Keyword
Program using range keyword along with an array
Output:
Explanation:
In this example, the for loop iterates over an array and prints every number present in the array. This is done using the range keyword which specifies how many times the loop is to be executed which in this case is the length of an array.
Example 4: Infinite for loop
Program to infinitely iterate through the loop
Output:
Explanation:
In this example, as there is no flow control, the condition is always true so it will work as an infinite loop.
Conclusion
- Loop in programming is used for repeating the block of code for a finite or infinite duration of time.
- For loop in Golang is used for repeating a block of code until a condition specified in the loop is met. For is the only loop available in Golang to execute a block of code repeatedly.
- The syntax is:
- In this, all three parameters are optional.