Three forms of Go language for

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.