Updated April 14, 2023
Introduction to go for loop
Loop in go language allow developers to avoid writing same peace of code for execution of same calculations, there is only one loop function which is for loop. It contains three attributes like initialization (we can initialize the some variables like i:=0), conditional (here we can put some conditions on which the execution of the loops will be done, so if condition is successful means body of for loop will be executed like i<10), increment or decrement (in this the value of the variable will change on each iteration for example i++ or i– we can also use pre like –i and ++i), loop alone play role of while and infinite loops.
How do for loop Functions work in go Language?
The working of the loop function, we will take one simple syntax for it.
num=0
for i:0; i<10; i++{
num=i+num
}
In the above sample, we first see each attribute of the for function.
- for: For is the system-defined function, once the go compiler reads about the for it will understand that there is looping work going to happen. And compiler will expect some initialization, conditions, and increment or decrement of the variables.
- i: This attribute is basically used to initialize the initial value for the variables. For example suppose we want to check array of students details in that case we will require to start the array from beginning to the end of the array to get details of the each students. And we know array attribute can be access from its index value which start with 0 and consequently increase the value like 1, 2, 3, 4, 5 etc . Now to access the each attribute of the index we will take one variable and we will start it with 0 and keep increasing the value of i until the end of the array.
- i++: This is the increment part, we can take it as i– also which means decrement. We can decide on the basis of our requirement for increment ++i or decrement –i. We can see this with example like, suppose we have list of student array and we want to traverse all the array of student from start to end then we may require some thing which will keep changing on each iteration. This increment will get affected on the end of every execution of the loop. Last affected value of the variable will not enter into the loop body part as here condition will be failed and this loop will be exited.
- body: The body allow us to write the logic which we wanted to execute over each iteration of the loop. Let us take an example suppose we want to add 5 marks extra to each students of the array list then we will run the loop and over each iteration inside the body we will add 5 marks to each student number. So this adding of 5 marks will be done in this loop body section.
Types of for loop Functions
Given below are the types mentioned:
1. Plain for loop function
Using for loop as the for loop, with all its three attributes.
- In the below example, we can see we are performing the addition of the number from 0 to 11 and printing the output of them.
- We have taken j:=0 which means we are initializing the value of the variable j.
- And we have set a condition of j<11 which means the loop will continue till the value of the j is less than 11.
- Finally we have taken the increment part which j++, which means on each iteration of the loop the value of the j will get affected by 1.
Code:
package main
import "fmt"
func main() {
add := 0
for j := 0; j < 11; j++ {
add += j
}
fmt.Println("The addition of number upto 11 is",add)
}
Output:
2. Using the loop as infinite loop
Because in this loop we have not taken any condition it will continue till infinite, it may affect your system.
Code:
package main
import "fmt"
func main() {
for {
fmt.Println("Birth and death will continue till infinite")
}
}
Output:
3. Using the “for loop” function as the while loop
If you are aware of the while loop so here we are using the while loop concept, there is no keyword of while. In the below example we have taken a condition where we are assuming the retirement age of any person is 60 years and we have taken initial value of i which means start of job is 55 and hence the loop executed till person reaches at the age of 60.
Code:
package main
import "fmt"
func main() {
var i=55
for i< 60{
fmt.Println("At age of 60 you will retired")
i++
}
}
Output:
4. Using this loop function as the range
It contains the range value with various names and we’re printing the range value and the position of the range.
Code:
package main
import "fmt"
func main() {
rangevariable:= []string{"Ranjan", "Ajay", "Vijay"}
for index, value:= range rangevariable {
fmt.Println(index, value)
}
}
Output:
Recommended Articles
This is a guide to go for loop. Here we discuss the introduction, how does the loop functions work, and 4 types with programming examples. You may also have a look at the following articles to learn more –