While loop in golang

A while loop in Go is a control flow statement that allows a certain block of code to be executed repeatedly until a specified condition is no longer true. Here is an example of a while loop in Go:

package main

import "fmt"

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

Explanation: 1. The first line package main declares that this Go file is part of the main package. 2. The second line import "fmt" imports the fmt package, which provides functions for formatted I/O operations. 3. The func main() function is the entry point of the program. 4. i := 1 declares a variable i and initializes it with the value 1. 5. The for loop starts with the keyword for followed by the condition i <= 5. This loop will continue executing as long as the condition is true. 6. Inside the loop, fmt.Println(i) prints the value of i to the console. 7. i++ increments the value of i by 1 in each iteration. This is equivalent to i = i + 1. 8. Once i becomes greater than 5, the condition i <= 5 becomes false, and the loop stops executing.

The output of the above program will be:

1
2
3
4
5

This is because the loop will execute as long as the value of i is less than or equal to 5, printing the value of i in each iteration.