Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go Language Loop Statements

Go Language Loop Statements

In many practical problems there are numerous regular repetitive operations, so programs need to execute certain statements repeatedly.

The following is a flowchart of loops in most programming languages:

Go provides the following types of loop processing statements:

Loop types Description
for loop Repeatedly execute a block of statements
Nested loops Nest 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.

The Go language supports the following loop control statements:

Control statement Description
break statement Often used to interrupt the current for loop or break out of a switch statement
continue statement Skip the remaining statements in the current loop, then continue to the next iteration.
goto statement Transfer control to a labeled statement.

Infinite loop

If the conditional statement in a loop is never false, an infinite loop occurs; we can execute an infinite loop by setting only one conditional expression in the for loop statement:

Example

package

main

import

"fmt"

func

main

()

{

   

for

true

 

{

        fmt

.

Printf

(

"This is an infinite loop.\n"

);

   

}

}