3rd lesson Go control, loop, select, jump statement

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
 PackageMainImport("FMT")funcMain () {/* Go retains pointers, but unlike other programming languages, pointer operations and the "-to" operator are not supported in go, but are directly "." Selector to manipulate the pointer target object's member operator "&" to take the variable address, using "*" to indirectly access the target object by pointer to the default nil instead of NULL * /A: =1    varP *int= &a FMT. Println (*P)//Output 1    //Hands    /* + + and--is used as a statement instead of an expression expression can be placed on the = right so now a++ can only be used as a separate line */a--FMT. Println (*P)//Output 0    if1<2{//Zoda brackets must be placed on the IF same line}a usage of//if    ifA: =1; A >1{//initialization statement; Conditional Statement}Another use of//if, note that the scope of a is only in the IF statement block    //And overwrite a above, when the if executes, a becomes an external statement of a    /* Loop statement for-go only for one loop statement keyword, but supports three forms of initial-initialization and stepping expressions can be multiple values-The conditional statement is re-examined each time, so it is not recommended in the conditional language Use a function in a sentence, try to calculate the condition in advance and replace it with a variable or constant-the opening brace must be in the same line as the conditional statement */Fmt. Println ("-----") AA: =1     for{aa++ifAA >3{ Break} FMT. PRINTLN (AA)} FMT. Println ("Over")//First formFmt. Println ("-----") forAA <=3{aa++ FMT. PRINTLN (AA)}//Second formFmt. Println ("-----") forI: =0; I <3; i++ {fmt. Println ("The Third Kind")    }//Third form (step expression)    / * SELECT statement Switch-you can use any type or expression as a conditional statement-no write break required-if you want to continue with the next case, you need to use the Fllthrough statement-support An initialization expression (which can be in parallel) with a semicolon-left brace must be on the same line as the conditional statement * /Fmt. Println ("-----") B: =1    SwitchD = Case0: FMT. Println ("A=0") Case1: FMT. Println ("A=1")default: FMT. Println ("None")} FMT. Println ("-----")SwitchBB: =1; {//The scope of this BB is also within the code block     CaseBB >=0: FMT. Println ("Bb>=0") CaseBB >=1: FMT. Println ("Bb>=1")default: FMT. Println ("None")} FMT. Println ("-----")/ * Jump statement goto,break,continue-Three syntax can be used with the label-the label name is case-sensitive, if you create a label without using a compile error-break and continue mate tag can be The bounce-goto for multi-layer loops is the adjustment execution position, which is not the same as the result of the other 2 statement mates */LABEL1: for{ forI: =0; I <Ten; i++ {ifi >3{ BreakLABEL1}}} fmt. Println ("Jump out of the loop!")//Using the break LABEL1 will jump out of the same level of rotation as the LABEL1, noting that the same level of loops as LABEL1    //Obviously if you use Goto, you will adjust the execution position, infinite loop down    //If you use continue, you will also jump out of the LABEL1 level loop, where there is an infinite loop, which must be adjusted as follows:LABEL2: forI: =0; I <Ten; i++ { for{ContinueLABEL2 FMT. Println (i)//This clause will never be executed}} FMT. Println ("Jump out of the loop!")//If you change contain to Goto, the result will be an infinite loop! }

Operation Result:
1
0
—–
2
3
Over
—–
—–
Third Kind
Third Kind
Third Kind
—–
A=1
—–
Bb>=0
—–
Jump out of the loop!
Jump out of the loop!

Related Article

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.