Loops in GoLang

Similar to other programming languages, GoLang has its own way of looping or repeating a block of codes. However, there is only one loop in GoLang, which is for loop. In GoLang, for loop has different forms for different use cases. Below is the basic structure of a for loop in GoLang:

for initialization; condition; post-action {
	// codes to be execute in here
}

As from what we can see above, the structure is pretty similar to standard for loops of other programming languages. initialization is where we initialize the variables to be used within the loop. condition takes a boolean that informs the loop to exit when the value is false. post-action is the action to be executed at the end of the code block. Usually, post-action is meant to update the variable/s set in initialization. Let's see an example below:

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

Output:

0
1
2
3
4

As we can see here, we initialized the variable i with the value 0. As long as the value of i is less than 5, we continue an print the value in the console. After each code block or after each time we print the value, we increment the value of i using the i++ post-action.


Other Forms of Loops

package main

import "fmt"

func main() {
	fmt.Println("-------- Basic For Loop --------------")

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

	fmt.Println("-------- Break and Continue --------------")

	// Break and continue
	for i := 0; i < 10; i++ {
		if i <= 3 {
			continue
		}

		if i >= 7 {
			break
		}
		fmt.Println(i)
	}

	fmt.Println("-------- While Loop in GoLang --------------")

	// while equivalent
	x := 2
	for x < 10 {
		fmt.Println(x)
		x *= 2
	}

	fmt.Println("---------- Infinite Loop ------------")

	// Inifinite loop
	y := 1
	for {
		if y > 10 {
			break
		}
		fmt.Println(y)
		y *= 2
	}
}

GoLang Playground Link

Result:

-------- Basic For Loop --------------
0
1
2
3
4
-------- Break and Continue --------------
4
5
6
-------- While Loop in GoLang --------------
2
4
8
---------- Infinite Loop ------------
1
2
4
8

Basic For Loop

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

This is similar to what we have just discussed previously. It has the initialization, condition, and post-action. This is a typical form of a for loop that is similar to other programming languages. The result will be as follows:

0
1
2
3
4

Break and Continue

Aside from the condition, break and continue are two other ways of controlling the flow or execution of a for loop. break stops or exits the for loop. While continue, prevents the execution of other lines in the block and starts by executing the post-action and it will return to the beginning of the code block. Let's examine the example below:

// Break and continue
for i := 0; i < 10; i++ {
	if i <= 3 {
		continue
	}

	if i >= 7 {
		break
	}
	fmt.Println(i)
}

Result:

4
5
6

Explanation:

As long as the value of i is less than or equal to 3, it will execute continue. This prevents the execution of the remaining lines of codes in this code block. When the value of i is 4, it will not enter the if conditions, thus, it will execute the print line. When the value of i reaches 7, it will execute break which will then terminate the loop.

While Loop

In other programming languages, such as Java, we specifically have a while loop. However, GoLang doesn't have this for simplicity but the structure is almost the same which is as follows:

for condition {
	// codes to execute
}

Example:

// while equivalent
x := 2
for x < 10 {
	fmt.Println(x)
	x *= 2
}

Result:

2
4
8

From here, we can see that initialization and post-action can be skipped. The only thing being considered here is the condition and it has to have a value of true in order for the code block to be executed.

Infinite Loop

// Inifinite loop
y := 1
for {
	if y > 10 {
		break
	}
	fmt.Println(y)
	y *= 2
}

Result:

1
2
4
8

In the example above, we can see that we can even remove the condition as well. We can move the checking or the control of exiting the loop within the code block.