Logo

Developer learning path

Go

Loops in Go

Loops

68

#description

Loops are an essential concept in all programming languages, including Go. In Go, there are mainly three types of loops: for, while, and do-while.

  1. For loop:

The most commonly used loop is the for loop in Go.

The syntax of for loop in Go is:

                    
for initialization; condition; increment {
    // statements
}
                  

Here, initialization is executed only once before the loop starts. The condition is checked at the beginning of each iteration. If it is satisfied, the statements in the loop body are executed. Then the increment is executed, and the condition is checked again. This process continues until the condition becomes false.

Example:

                    
for i := 1; i <= 10; i++ {
    fmt.Println(i)
}
                  
  1. While loop:

Go language does not provide a separate while loop like other languages. However, we can use for loop as a while loop in Go.

The syntax of the while loop using the for loop is:

                    
for condition {
    // statements
}
                  

Here, the condition is checked at the beginning of each iteration, and if it is satisfied, the statements in the loop body are executed.

Example:

                    
i := 1
for i <= 10 {
    fmt.Println(i)
    i++
}
                  
  1. Do-while loop:

Go language does not provide a separate do-while loop like other languages. However, we can use for loop as a do-while loop in Go.

The syntax of the do-while loop using the for loop is:

                    
for {
    // statements
    if condition {
        break
    }
}
                  

Here, the statements in the loop body are executed first, and then the condition is checked. If it is satisfied, the loop continues; otherwise, the loop ends.

Example:

                    
i := 1
for {
    fmt.Println(i)
    i++
    if i > 10 {
        break
    }
}
                  

These are the types of loops in Go. You can use them based on your requirements to iterate over a set of values repeatedly.

March 27, 2023

41

#description

In Go, loops are used to execute a block of code repeatedly based on a certain condition. There are two types of loops in Go: for loop and range loop.

For Loop:

The for loop is used when we have a predefined number of iterations, where the loop runs until a specific condition is satisfied.

The syntax of a for loop in Go is as follows:

                    
for initialisation; condition; post {
    //Code to be executed repeatedly
}
                  
  • The initialization statement executes before the loop starts.
  • The condition statement checks if the condition is true or false.
  • The post statement executes after each iteration of the loop.

Example:

                    
for i := 0; i < 5; i++ {
    fmt.Println(i)
}
                  

Output:

                    
0
1
2
3
4
                  

Range Loop:

The range loop is used when we need to iterate over arrays, slices, strings, maps, and channels. It provides the indices and their corresponding values. It is used to iterate over collection data types.

The syntax of a range loop in Go is as follows:

                    
for index, element := range collection {
  // Code to be executed repeatedly
}
                  
  • The index integer variable holds the index of the current element.
  • The element variable holds the value of the current element.

Example:

                    
fruits := []string{"apple", "banana", "mango"}
for index, fruit := range fruits {
    fmt.Printf("Index: %d, Fruit: %s\n", index, fruit)
}
                  

Output:

                    
Index: 0, Fruit: apple
Index: 1, Fruit: banana
Index: 2, Fruit: mango
                  

Loops are an essential part of programming, and understanding them is crucial for any developer.

March 27, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.