Go language loop statement


Go language loop statements

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

The following is the flow chart of loop programs in most programming languages: loop_architecture.jpg

Go language provides the following types of loop processing statements:

Loop typeDescription
for loopRepeatedly execute statement block
Loop nestingNest 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 loop control statements:

Control statementDescription
break statement is often used to interrupt the current for loop or jump out of the switch statement
continue statement skip the current the remaining statements of the loop, and then continue with the next round of the loop.
goto statement Transfers control to the marked statement.

Infinite Loop

If the conditional statement in the loop is never false, an infinite loop will occur. We can use the for loop statement to only Set a conditional expression to perform an infinite loop:

package main

import "fmt"

func main() {
    for true  {
        fmt.Printf("这是无限循环。\n");
    }
}