You are currently viewing GoLang Looping: Everything You Need to Know

GoLang Looping: Everything You Need to Know

Loops are an essential part of any programming language, including GoLang. They allow you to repeat a block of code multiple times, making it easier to perform repetitive tasks or iterate over collections of data. This article explores the different types of loops available in GoLang, along with their control statements. We’ll provide code examples and explanations to help you understand how to effectively use looping in GoLang.

The for Loop

The for loop is the most commonly used loop in GoLang. It allows you to repeat a block of code a specific number of times or until a certain condition is met. Here’s the general syntax:

for initialization; condition; post {

    // code to be executed
    
}

Initialization is an optional statement used to initialize variables before the loop begins. Condition is the condition that is evaluated before each iteration of the loop. If the condition evaluates to true, the loop continues; otherwise, it terminates. And post is a statement to be executed after each iteration of the loop.

Let’s see an example that prints numbers from 1 to 10 using a for loop:

import "fmt"

func main() {

	for i := 1; i <= 10; i++ {

		// Print the current value of i
		fmt.Println(i)
		
	}

}

Let’s see another example that demonstrates summing numbers from 1 to 10 using a for loop:

import "fmt"

func main() {

	// Holds the sum of numbers
	sum := 0
	
	for i := 1; i <= 10; i++ {

		// Add current value of i to sum
		sum += i

	}

	// Print the sum to the console
	fmt.Printf("The sum of numbers from 1 to 10 is %d.", sum)

}

The while Loop

Unlike some other programming languages, GoLang does not have a dedicated while loop. However, you can simulate it using a for loop without the initialization and post statements. The loop continues as long as the condition is true. Here’s an example:

Let’s see an example that prints numbers from 1 to 10 using a simulated while loop:

import "fmt"

func main() {

	i := 1

	for i <= 10 {

		// Print the current value of i
		fmt.Println(i)

		// Add 1 to i
		i++

	}

}

Let’s see another example that demonstrates summing numbers from 1 to 10 using a simulated while loop:

import "fmt"

func main() {

	// Holds the sum of numbers
	sum := 0

	i := 1

	for i <= 10 {

		// Add current value of i to sum
		sum += i

		// Add 1 to i
		i++

	}

	// Print the sum to the console
	fmt.Printf("The sum of numbers from 1 to 10 is %d.", sum)

}

The do-while Loop

Similarly to the while loop, GoLang does not have a built-in do-while loop. However, you can achieve the same effect using a for loop and a break statement.

Let’s see an example that prints numbers from 1 to 10 using a simulated do while loop:

import "fmt"

func main() {

	i := 1

	// This basically is an infinite loop
	// And is guaranteed to at least run once
	for {

		// Print current value of i
		fmt.Println(i)

		// Add 1 to i
		i++

		if i > 10 {

			// Exit loop when i becomes greater than 10
			break

		}

	}

}

Let’s see another example that demonstrates summing numbers from 1 to 10 using a simulated do while loop:

import "fmt"

func main() {

	// Holds the sum of numbers
	sum := 0

	i := 1

	// This basically is an infinite loop
	// And is guaranteed to at least run once
	for {

		// Add current value of i to sum
		sum += i

		// Add 1 to i
		i++

		if i > 10 {

			// Exit loop when i becomes greater than 10
			break

		}

	}

	// Print the sum to the console
	fmt.Printf("The sum of numbers from 1 to 10 is %d.", sum)

}

Loop Control Statements

GoLang provides several control statements that can be used to alter the behavior of loops.

The break Statement

The break statement terminates the loop immediately and transfers control to the statement following the loop. It allows you to exit a loop prematurely, terminating the loop’s execution. It is commonly used when you want to exit a loop based on a certain condition.

Let’s see an example that demonstrates exiting a for loop when a specific condition is met. In our example, when the loop variable i becomes 5:

import "fmt"

func main() {

	for i := 1; i <= 10; i++ {
		
		if i == 5 {
			
			// Exit loop when i is 5
			break
			
		}
		
		// Print value of variable i
		fmt.Println(i)
		
	}
	
}

The continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop. It allows you to skip the current iteration of a loop and proceed to the next iteration. It is useful when you want to skip certain elements or perform specific actions within a loop.

Let’s see an example that demonstrates skipping even numbers in a loop:

import "fmt"

func main() {

	for i := 1; i <= 10; i++ {

		if i%2 == 0 {

			// Move to next iteration when i is divisible by 2
			continue

		}
		
		// Print value of variable i
		// Only odd numbers, because we are skipping even numbers above
		fmt.Println(i)

	}

}

Conclusion

We covered the different types of loops available in GoLang, including the for loop, the simulated while loop, and the simulated do-while loop. We also explored loop control statements like break, and continue. By understanding and effectively using loops in GoLang, you can make your code more efficient and concise. Practice implementing loops and experiment with different control statements to become proficient in using looping constructs in GoLang.

Source

I hope you found this information informative. If you would like to receive more content, please consider subscribing to our newsletter!

Leave a Reply