GoLang - While Loop

2 minute read

In this tutorial, you will learn about “while Loop in Go Programming” with the help of example.

In Go programming, there is no explicit “while” loop construct like in some other programming languages. However, you can achieve the same functionality using a “for” loop with a modified syntax. In this tutorial, we will explore how to create a “while” loop in Go programming using the “for” loop. We will provide examples and explanations in simple and easy-to-understand language. So let’s dive in and learn about the “while” loop in Go!

Basic While Loop

To create a “while” loop in Go, we can use a “for” loop with just a condition and omit the initialization and increment statements.

Example:

package main

import "fmt"

func main() {
    i := 1
    for i <= 5 {
        fmt.Println(i)
        i++
    }
}

In this example, we have a variable 'i' initialized to 1. The “for” loop continues as long as the condition 'i <= 5' is true. Inside the loop, we print the value of 'i' using the 'fmt.Println' statement, and then increment 'i' by 1 using the 'i++' statement.

Infinite While Loop

To create an infinite “while” loop, we can use a “for” loop with just a condition that always evaluates to true.

Example:

package main

import "fmt"

func main() {
    for {
        fmt.Println("This is an infinite loop")
    }
}

In this example, we have a “for” loop without any condition specified. Without a condition, the loop will continue indefinitely, creating an infinite loop. Inside the loop, we print the message “This is an infinite loop” using the 'fmt.Println' statement. Be cautious when using infinite loops and ensure you have a proper way to terminate them.

Breaking Out of a While Loop

To exit a “while” loop based on a certain condition, you can use the 'break' statement.

Example:

package main

import "fmt"

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

In this example, we have a “while” loop that continues indefinitely. However, inside the loop, we have an 'if' statement that checks if 'i' is greater than 5. If the condition is true, we use the 'break' statement to exit the loop. This allows us to control the termination of the loop based on the value of 'i'.

By mastering the “for” loop and its modified syntax, you can effectively emulate the behavior of a “while” loop in Go programming. Practice using these concepts to handle repetitive tasks and control program flow in your Go programs.

Conclusion

We learned how to create a basic “while” loop and an infinite “while” loop. Additionally, we saw how to break out of a “while” loop using the break statement to control the loop’s termination based on a specific condition.

Although Go doesn’t have a dedicated “while” loop, we can achieve the same functionality using the “for” loop with a modified syntax. In this tutorial, we explored how to create a “while” loop in Go programming by omitting the initialization and increment statements, and relying solely on the loop condition!

Congratulations on completing this beginner’s guide to the “while” loop in Go programming! With this knowledge, you’re now equipped to create loops that repeat based on conditions and handle different scenarios in your code. So go ahead, experiment with “while”

Updated: