Go language loop statement
In many practical problems, there are many regular repetitive operations, soit is necessary to repeat some statements in the program.
The following is a flowchart of looping programs in most programming languages:
Go
language provides the following types of loop processing statements:
Cycle type |
Description |
---|---|
Break statement |
Repetitive execution statement block |
Continue statement |
Nesting one or more for loops within for loops |
Loop control statement
Loop control statements can control the execution of statements in the loop.
GO
language supports the following loop control statements:
Control statement |
Description |
---|---|
Break statement |
Often used to break the current for loop or jump out of switch statements |
Continue statement |
Skip the remaining statements of the current loop and proceed to the next loop. |
Goto statement |
Transfer control to the marked statement. |
Infinite cycle
If the conditional statement in the loop is never false
, there will be an infinite loop, and we can pass through for
only one conditional expression is set in the loop statement to execute the infinite loop:
Example
package main
import "fmt"
func main() {
for true {
fmt.Printf("This is an infinite loop.\\n");
}
}