Easy Tutorial
❮ Go Scope Rules Go Nested Loops ❯

Go Language Loop Statements

In many practical problems, there are repetitive operations with regular patterns, so certain statements need to be executed repeatedly in the program.

The following is a flowchart for loop programs in most programming languages:

Go language provides the following types of loop processing statements:

Loop Type Description
for Loop Repeats execution of a block of statements
Nested Loops Embeds one or more for loops within a for loop

Loop Control Statements

Loop control statements can control the execution process of statements within the loop body.

Go language supports the following types of loop control statements:

Control Statement Description
break Statement Often used to interrupt the current for loop or exit a switch statement
continue Statement Skips the remaining statements of the current loop and then continues with the next iteration
goto Statement Transfers control to the labeled statement

Infinite Loop

If the condition statement in the loop never becomes false, it results in an infinite loop. We can achieve an infinite loop by setting only one condition expression in the for loop statement:

Example

package main

import "fmt"

func main() {
    for true {
        fmt.Printf("This is an infinite loop.\n")
    }
}
❮ Go Scope Rules Go Nested Loops ❯