In we talk about loops in golang , then we have only one type of loop in golang i.e., for
loop. In golang there is no while
or do while
loop. So here we will see how we can use for
loop in different different scenario in Golang.
![for loop in golang](https://thecodedata.com/wp-content/uploads/2023/05/for-loop-in-golang.jpg)
For Loop Syntax in Golang
A for
statement specifies repeated execution of a code block. In golang we can use for
loop in the following forms –
Classic for loop
Classic / basic for loop in Golang has three component
- init statement (initialization)
- Condition Expression
- Post Statement (increment/decrement)
For Loop Syntax in Golang
for initialization ; condition ; post statement{
// code block
}
Golang code for printing ‘hello’ 10 times
package main
import "fmt"
func main() {
for i := 1; i <= 10; i++ {
fmt.Println("Hello")
}
}
In the above code, we are using classic / basic for loop for printing ‘Hello’ 10 times. In for statement, there is an initialization statement (i:=1) after that a condition (i<=10) after that post statement (i++). According to the given condition and post statement this for loop will run 10 times and print ‘Hello’.
Output
![for loop in golang](https://thecodedata.com/wp-content/uploads/2023/05/image-16.png)
for loop in Golang with single condition
There is no while loop in golang , but you can use for loop as while loop with single condition
Synatx
for condition {
//code block
}
the above statement is similar to the while loop in C/C++/Java etc.
Printing n natural number in Golang
package main
import "fmt"
func main() {
n := 100
i := 1
for i <= n {
fmt.Print(i)
fmt.Print(" ")
i++
}
}
In the above code, we are using for loop with a single condition (similar to a while loop), if this condition is true then the code block inside for loop will keep executing. Here we are printing n natural number. After Printing n natural number condition become false and for loop will be terminated
Output
![while loop in golang](https://thecodedata.com/wp-content/uploads/2023/05/image-15-1024x106.png)
- If the condition is missing in
for
statement then it’s equivalent to the boolean true, or we can say that without the condition this for loop will become an infinite loop. You can break an infinite loop with the ‘break’ keyword according to your requirement
for {
// code block
}
Printing n nautral number in Golang (infinite for loop)
package main
import "fmt"
func main() {
n := 10
i := 1
for {
if i >= n {
break
}
fmt.Print(i)
fmt.Print(" ")
i++
}
}
In the above code, we are using an infinite for loop for printing first n natural numbers (here n =10). In the for loop there is an if condition (i>=10), so if we already printed first n natural number, then the if condition will be true and we are breaking for loop here with the break
keyword in Golang.
Output
![infinity for loop](https://thecodedata.com/wp-content/uploads/2023/05/image-17.png)
for loop as for each loop in Golang
In Golang, there is no for-each
loop like in some other languages Java etc. but we can use for loop as for each loop for iterating array, map in golang by using range
.
Iterating array in Golang using for loop
package main
import "fmt"
func main() {
array := [5]int{1, 2, 3, 4, 5}
// Iterating over the array using range
for index, value := range array {
fmt.Printf("index: %d, value: %d\n", index, value)
}
}
Output
![for each loop in golang](https://thecodedata.com/wp-content/uploads/2023/05/image-38.png)
6 thoughts on “For Loop In Golang”