[Golang note] Process Control

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Process Control

• Process Control Statement function

Select : Jump to a different execution sequence depending on the condition.

Loop : Executes a sequence repeatedly based on conditions.

Jump : Returns to an execution sequence according to the condition.

• Process Control Statement types

Conditional Statement : The keyword is if, else, and else if.

SELECT statement : The keyword is switch, case, and select.

Loop statement : The keyword is for and range.

Jump Statement : the keyword is goto.

Conditional statements

• The syntax is as follows

  //1
If
Condition {...}
//2 ifcondition {...}Else { ...}
//3 ifcondition {...}else ifCondition {...}
//4 ifcondition {...}else ifcondition {...}Else { ...}

• Attention points

Conditional statements do not need to use parentheses () to enclose the condition.

A conditional statement must exist, regardless of several statements in the body of the statement, the curly braces {} .

The left curly brace of the conditional statement { must be in the same row as if or else , or the compiler gives an error: syntax error:missing {after if clause.

The else of the conditional statement must be in the same row as the closing curly brace of the last statement body, or the compiler gives an error: syntax error:unexpected else, expecting}.

A conditional statement can add a variable initialization statement after the if , but it requires a semicolon ; Interval.

if 1 1 {    FMT. Println ("OK")}

SELECT statement

• The syntax is as follows

SwitchCondition { Case V0:    ... Case v1:    ... Case v2:    ... Case v3:    ... Case V4,V5,V6:    ...default:    ...}

• Attention points

In the SELECT statement, the left curly brace { must be in the same row as the switch .

In a SELECT statement , a constant can be used after a case, and a variable can be used.

Package   mainimport "FMT" func main () {condition:=0Casevalue:=0    SwitchCondition { Casecasevalue:fmt. Printf ("0")     Case 1: FMT. Printf ("1")    default: FMT. Printf ("Default")    }}

in the SELECT statement, a single Case multiple result options can appear after the

In the SELECT statement, you do not need to use break to explicitly exit a case.

In the SELECT statement, the next case continues only if the fallthrough keyword is explicitly added in the case.

Package   mainimport "FMT" func main () {condition:=1    SwitchCondition { Case 0: FMT. Printf ("0")     Case 1: FMT. Printf ("1") Fallthrough default: FMT. Printf ("Default")    }}

        in the SELECT statement,Don't setSwitchafter the conditional expression, in which case the entireSwitchStructure andif...Else... The logical equivalence.

Package   mainimport "FMT" func main () {num:=1    Switch {     Case 0<= num && num <=3: FMT. Printf ("0-3")     Case 4<= num && num <=6: FMT. Printf ("4-6")     Case 7<= num && num <=9: FMT. Printf ("7-9")    }}

Jump statement

• The syntax is as follows

Goto Label

• Attention points

The semantics of the SELECT statement is very simple, which is to jump to a tag within this function to execute.

   package   Main  import     "FMT"     func   Main () {i:  = 0           for    {i  + +   if   i >= 10   {   Goto    finish }}finish : FMT. Println (   

Looping statements

• The syntax is as follows

// Normal Cycle  for 0 N; i++ {    ...} // Array Loops  for I, V: =  range array {    ...} // Infinite Loops  for  {    ...}

• Attention points

The loop statement supports only for structures and does not support the while and do-while structures.

Loop statement the left curly brace { must be in the same row as for.

Loop statements do not support multi-assignment statements that are separated by commas, and support the use of multivariate assignments to initialize multiple variables.

Package   mainimport "FMT" func Main () {array:= []int{1,2,3,4,5,6}     forI, J: =0,Len(Array)-1; I < J; I, j = i+1, J-1{array[i], Array[j]=Array[j], Array[i]} fmt. Println (Array)}

The Loop statement supports continue and break to control loops, but provides a more advanced breaking and optionally interrupts which layer loops.

Package   mainimport "FMT" func Main () {JLoop1://tag Loop 1, no non-cyclic code here, break label jumps out of Loop 1     forJ: =0; J <5; J + +{ JLoop2://tag Loop 2, no non-cyclic code here, break label jumps out of Loop 2         forI: =0; I <Ten; i++ {            ifJ <3 {                 BreakJLoop2//Jump out of the Loop 2            }            ifi >5 {                 BreakJLoop1//Jump out of the Loop 1} fmt. Println (J, i) }}
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.