(Go) 4. Process control

if else (branched structure)

Go language most commonly used with a flow control ifand for, and switch, and gotoprimarily to simplify the code, the code decrease repetition born structure, belonging to class flow control expansion

else ifAnd elseare optional

func ifDemo1() {
    score := 65
    if score >= 90 {
        fmt.Println("A")
    } else if score > 75 {
        fmt.Println("B")
    } else {
        fmt.Println("C")
    }
}

Special wording

ifDemo2 FUNC () {
     if Score: = 65 ; Score> = 90 {   // Score belongs inside if local variables , the call will be given out if 
        fmt.Println ( " A " ) 
    } the else  if Score> 75 { 
        FMT .Println ( " B " ) 
    } the else { 
        fmt.Println ( " C " ) 
    } 
fmt.Println (Score) // undefined: Score }

for (loop structure)

The initial statement -> conditional expression -> loop statement -> End Statement

for initial statement; conditional expression; {end of statement 
    loop statement 
}

Conditional expression returns the truebody of the loop have been conducting cycles until the condition expression returns falseautomatically exit the cycle time.

func forDemo() {
    for i := 0; i < 10; i++ {
        fmt.Println(i)
    }
}

The initial statement can be ignored

func forDemo2() {
    i := 0
    for ; i < 10; i++ {
        fmt.Println(i)
    }
}

The initial statement and end statement can be omitted

func forDemo3() {
    i := 0
    for i < 10 {
        fmt.Println(i)
        i++
    }
}

Infinite loop, you can break, goto, return, panicstatement forces withdraw from circulation.

for { 
    loop statement 
}

for range

  • Arrays, slices, and the index value string is returned.
  • map returns keys and values.
  • Channel (channel) value in the return channel only.
    S: = " Hello Hello " 
    for index, value: = Range S { 
        fmt.Printf ( " % C% D " , index, value) // 0. 1 E 2 L H L. 4. 3 O 8. 5 good you 
    }

 

 

 

 

 

 

 

 

--- gentleman at the fact, not at its China; its internal governance, died outer    Jang   ----

Guess you like

Origin www.cnblogs.com/jiangzongyou/p/12159218.html