Loops in Golang3 min read

Loops in Golang

As for the loops in Golang, there is only one loop available and that is “for loop”. There are no loops such as “while” and “do-while” in Golang as there are in other programming languages like C or Java.

For Loop

A “for loop” is a loop that repeats itself until the condition becomes false. It allows us to write a loop that needs to be executed a specific number of times. Iteration is the term used to describe the process of repeating a loop.

Syntax:

for initialization; condition; post{
  statement(s);
}

Where,

  • Initialization: here we initialize a variable that has to be used in the for loop
  • Condition: here a condition has to be given which will define how many times the loop will be executed. If the conditions becomes false at any given time of the execution, the for loop will be exited.
  • Post: here we can define increment or decrement for the variable that we initialized earlier.

Example:

package main
import "fmt"
func main(){
  for i:= 0; i < 5; i++ {
    fmt.Println("i = ", i)
  }
}

Output:

C:\Users\Hiberstack\GoPrograms>go run ForLoopDemo1.go
i =  0
i =  1
i =  2
i =  3
i =  4
for loop in golang

We can even initialize multiple variables, define multiple conditions, and also define multiple post types in a single for loop.

Example:

package main
import "fmt"
func main(){
  for i, j := 5, 10; i <= 15 && j <= 20; i, j = i+1, j+1 {
    fmt.Println(i, " * ", j, " = ", i * j)
  }
}

Output:

C:\Users\Hiberstack\GoPrograms>go run ForLoopDemo2.go
5  *  10  =  50
6  *  11  =  66
7  *  12  =  84
8  *  13  =  104
9  *  14  =  126
10  *  15  =  150
11  *  16  =  176
12  *  17  =  204
13  *  18  =  234
14  *  19  =  266
15  *  20  =  300

Infinite Loop

We can create an infinite loop in Golang. Following is the syntax of it

Syntax:

for {
  statement(s);
}

Example:

package main
import "fmt"
func main(){
  for  {
    fmt.Println("Hello World!!!")
  }
}

Output:

C:\Users\Hiberstack\GoPrograms>go run ForLoopDemo3.go
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
Hello World!!!
.
.
.

As you can see from the output above, the message will be printed indefinitely unless the program is manually halted.


Nested for loop

We can create a “nested for loop” in Golang by nesting one “for loop” inside another “for loop.” Following is the syntax of nested for loop

Syntax:

for initialization; condition; post{
for initialization; condition; post{
        statement(s);
}
    statement(s);
}

Example:

package main
import "fmt"
func main(){
  for i := 2; i < 3; i++ {
    for j := 1; j <= 10; j++ {
      fmt.Println(i, " * ", j, " = ", i * j)
    }
    fmt.Println("This was nested for loop demo")
  }
}

Output:

C:\Users\Hiberstack\GoPrograms>go run ForLoopDemo4.go
2  *  1  =  2
2  *  2  =  4
2  *  3  =  6
2  *  4  =  8
2  *  5  =  10
2  *  6  =  12
2  *  7  =  14
2  *  8  =  16
2  *  9  =  18
2  *  10  =  20
This was nested for loop demo

Share:

Leave a Reply