Table of contents
1.
Introduction
2.
For Loop 
2.1.
Explanation For Loop
2.2.
Example
3.
For Loop as an Infinite Loop
3.1.
Explanation
3.2.
Example
4.
For Loop as a While Loop
4.1.
Explanation
4.2.
Example
5.
For Loop as a Range
5.1.
Explanation
5.2.
Example
6.
Flow Diagram
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Go Loops

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This article will be covering loops in Golang. We all know that we use loops to repeat a specific task. The word loops come with something circular. It's like if the task is done repeatedly, we use loops. Loops are generally used to repeat a more than capable task.

For Loop 

This is similar to the one we used in Java, C, C++, etc.

Syntax 

for initialization; condition; post{
//statement…
}

Explanation For Loop

  1. The initialization statement is always mentioned before the loop start. It may be a simple variable declaration, increment, or assignment operator. 
  2. Then the condition statement consists of a boolean expression, which is always checked at the beginning of the loop. If the condition becomes true, then the loop will continue. Otherwise, it will terminate.
  3. The posted statement executes after the body of the for-loop gets executed. After this statement, the condition statement gets re-evaluated. If its value turns out to be false, then the loop ends. 

Example

package main
 
import "fmt"  
 
func main() {      
    for i := 0; i < 4; i++{
      fmt.Printf("Coding Ninjas\n")  
    }
  }

 Output

Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas

We have taken a simple for loop in this example and printed it according to the condition. 

Must Read What are Loops in Java.

For Loop as an Infinite Loop

In this example, we take the for loop as an infinite loop.

Syntax

for{
//Statement…
}

Explanation

We have used it as an infinite loop in this loop by removing all three expressions from the for a loop. When we do not write the condition statements in the loop, the condition is true, and the loop goes to infinity.

Example

package main
 
import "fmt"

func main() {   
    for {
      fmt.Printf("Coding Ninjas\n")  
    }
}

 Output

Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
Coding Ninjas
.............

For Loop as a While Loop

Syntax 

for condition{
// statement…
}

Explanation

For loop can also work as a while loop. This loop continues to execute till the condition is met. When the condition fails, the loop ends.

Example

package main

import "fmt"
  
func main() {
    i:= 0
    for i < 3 {
       i += 2
    }
  fmt.Println(i)
}

 Output

4

We used a simple while loop to print the number in this loop.

For Loop as a Range

When we need to iterate through a slice or array of items, we use a range function along with it. This makes code much more straightforward.

 Syntax

For i,j:= range variable{
// statement..
}

Explanation

  1. The variables in which the values of the iteration are assigned. They are also known as iteration variables.
  2. The variable j is optional.
  3. This range expression is evaluated at the beginning of the loop.

Example

package main
 
 import "fmt"
 
 func main() {
    rvariable:= []string{"coding", "Ninjas", "Library"  
    for i, j:= range rvariable {
       fmt.Println(i, j)
    }  
}

Output

0 coding
1 Ninjas
2 Library

In this example, we have used a range for loops. Here we used to print the string against the integer.

Flow Diagram

FAQs

  1. What is the syntax of for loop in the Go language?
    The syntax is - for[condition}(init; condition; increment)| Range]{statement(s);}. The flow of control in a for loop is as follows. If the condition is valid, then the loops will execute.
     
  2. What happens to the flow of control after a for loop?
    If it satisfies the condition, the body of the loop gets executed. If it is false, the body does not execute, and the flow of control jumps to the following statement and so on.
     
  3. Do we need to put a statement after a for loop?
    We are not required to put any statement as long as the semicolon appears. Next, the condition gets evaluated; if this is true, then the loop's body gets executed.

Key Takeaways

In this article, we have extensively discussed loops and their implementation in go. We also explained loops and their different categories. We also explained their syntax along with examples and their explanation.

We hope that this blog has helped you enhance your knowledge regarding loops and if you would like to learn more, check out our articles on  Loops in C and Loops in Python. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass