Other control statements that loop through in Go are the only for. For the same is also more flexible:
Package Main
Import "FMT"
Func Main () {
One of the most basic, single-condition loops
This is a while loop that can replace other languages
I: = 1
For I <= 3 {
Fmt. Println (i)
i = i + 1
}
Classic cyclic condition initialization/conditional judgment/post-cycle conditional change
For j: = 7; J <= 9; J + + {
Fmt. Println (j)
}
An unconditional for loop is a dead loop unless you use break to jump out of a loop or
Return from function using return
for {
Fmt. Println ("Loop")
Break
}
}
loop control statements can control the execution of statements within a loop body, including:
Break statement: Often used to interrupt the current for loop or jump out of a switch statement
Continue statement: Skips the remaining statements of the current loop, and then proceeds to the next round of loops
Goto statement: Transfer control to the marked statement
**********************************
If the conditional statement is never false in an over-loop, an infinite loop is made, and we can perform an infinite loop by just setting a conditional expression in the FOR Loop statement:
Package Main
Import "FMT"
Func Main () {
For {//here can write true or omit
Fmt. Printf ("This is a dead loop oh ... \ n")
}
}
For loop, executing the CONTINUE statement triggers the execution of the for increment statement
package mainimport "FMT" Func main () { /* define local variables */ var a int = 10 /* for Cycle */ for a < 20 { if a == 15 { /* Skip this cycle */ a = a + 1; continue ; } fmt. Printf ("a value is : %d\n", a); a++; } }
The goto statement of the Go language can be transferred unconditionally to the line specified in the procedure.
A goto statement is typically used in conjunction with a conditional statement. can be used to achieve conditional transfer, form a cycle, jump out of the loop body and other functions.
Package Mainimport "FMT" Func Main () {/* define local variable */var a int = 10/* Loop */Loop:for a < {if a = = 15 { /* Skip Iteration */a = a + 1 goto LOOP} fmt. Printf ("A has a value of:%d\n", a) a++}}
This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1962681
Three forms of Go language for