Go from Beginner to Expert: A Complete Guide to Learn Golang PART-6 Step-by-Step Guide to understand Control Statements(for loops) of Golang

Go from Beginner to Expert: A Complete Guide to Learn Golang PART-6

Step-by-Step Guide to understand Control Statements(for loops) of Golang

Md. Faiyaj Zaman
3 min readJan 10, 2023

--

Welcome back to this tutorial on control statements in Golang. In the previous sections, we discussed if-else statements and switch statements. In this section, we will be discussing for loops in Golang.

To understand Control Statements(switch-case) of Golang, please check the previous post here.

For Loops

For loops are a type of control statement that allows you to repeat a block of code a specific number of times. They are a useful tool for iterating through lists, arrays, or other collections of data.

1Syntax and examples of using for loops in Golang

Here is the syntax for a for loop:

for initialization; condition; post {
// code to be executed
}

The initialization statement is executed before the loop starts. The condition is checked before each iteration of the loop. If the condition is true, the code in the loop is executed. If the condition is false, the loop ends. The post statement is executed after each iteration of the loop.

Here is an example of a for loop:

package main

import "fmt"

func main() {

for i := 0; i < 5; i++ {

fmt.Println(i)

}

}

In this example, the initialization statement i := 0 initializes the variable i with the value of 0. The condition i < 5 is checked before each iteration of the loop. As long as i is less than 5, the code in the loop will be executed. The post statement i++ increments the value of i by 1 after each iteration of the loop.

2Using for loops with range

You can also use the for loop as a while loop by omitting the initialization and post statements.

Here is an example of using a for loop as a while loop:

package main

import "fmt"

func main() {

i := 0

for i < 5 {

fmt.Println(i)

i++

}

}

In this example, the condition i < 5 is checked before each iteration of the loop. As long as i is less than 5, the code in the loop will be executed. The variable i is incremented by 1 after each iteration of the loop using the i++ statement.

3Using for loops as a while loop

You can also use the range keyword in a for loop to iterate over a list or array. The range keyword returns the index and value of each element in the list or array.

Here is an example of using the range keyword in a for loop:

package main

import "fmt"

func main() {

numbers := []int{1, 2, 3, 4, 5}

for i, num := range numbers {

fmt.Println("Index:", i, "Value:", num)

}

}

In this example, the for loop is iterating over the numbers array using the range keyword. The range keyword returns the index and value of each element in the array. The index and value are assigned to the variables i and num, respectively. The code in the loop is then executed for each element in the array, printing the index and value of each element.

These are just a few examples of how to use for loops in Golang. As you can see, they are a useful tool for iterating through lists, arrays, and other collections of data.

In the next section of this tutorial, we will be discussing break and continue statements in Golang.

To stay up-to-date on our latest posts, please click on the Follow button and subscribe to our email notifications.

Kindly explore the complete series on Golang, which presents a step-by-step guide, available here.

Happy Coding :)

--

--