Go if-else statement, for loop, switch statement

Go if-else语句

package main 

import  " fmt " 

func main () { 
    a: = 10 

    // 1 basic use
     // if a> 10 {
     // fmt.Println ( " greater than 10 " )
     //} else {
     // fmt.Println ( " Less than or equal to 10 " )
     // }

     // 2 if - else 
    // if a> 10 {
     // fmt.Println ( " greater than 10 " )
     //} else  if a == 10{
     // fmt.Println ( " equal to 10 " )
     //} else {
     // fmt.Println ( " less than 10 " )
     // }


     // 3 Note: You can't change the line (the end of each line in the go language, you need to add one;, Every time a newline is added, it will be added automatically;) The following is a demonstration of error
     // if a> 10 {
     // fmt.Println ( " greater than 10 " )
     //} else  if a == 10 
    // {
     // fmt.Println ( " Equal to 10 " )
     //} else {
     // fmt.Println ( " less than 10" )
     // }


     // 4 conditions can be initialized (with the difference in scope)
     if a: = 10; a <10 { 
        fmt.Println ( " xxx " ) 
    } else { 
        fmt.Println ( " yyy " ) 
    } 

    fmt.Println (a) 
}

Go for loop

// Loop (only for loop in Go, no while, do while ) 
package main 

import  " fmt " 

func main () {
     // 1Basic syntax
     // for initialization; condition judgment; self-increment / self-decrease {loop body code} All three parts can be omitted
     // Print 0--9
     for i: = 0; i <10; i ++ { 
        fmt.Println (i) 
    }

     // 2Omit the first part (initialization), the scope is different
     // i = 0
     // for ; i <10; i ++ {
     //     fmt.Println (i)
     // }

     // 3 omit the third part
     // for i: = 0; i <10 ; {
     // i ++ 
    //    fmt.Println (i)
     // }

     // 4 Omit the first and third parts
     // i: = 0
     // for ; i <10 ; {
     // i ++ 
    //     fmt.Println (i)
     // }

     // 5 for condition {}
     // i: = 0
     // for i <10 {
     // i ++ 
    //     fmt.Println (i)
     // }

     // 6 infinite loop
     // for {
     // fmt.Println ( " I am Infinite loop " )
     // }

     // 7 breakAnd continue 
    // for {
     // fmt.Println ( " I am an infinite loop " )
     //     break 
    // // continue 
    // } 
}

Go switch statement

package main

func main() {
    // 1 switch 基本使用
    //a := 10
    //switch a {
    //case 1:
    //    fmt.Println("1")
    //case 2:
    //    fmt.Println("2")
    //case 9:
    //    fmt.Println("9")
    //case 10:
    //    fmt.Println("10")
    //}

    // 2 default
    //a := 15
    //switch a {
    // case 1 :
     // fmt.Println ( " 1 " )
     // case 2 :
     // fmt.Println ( " 2 " )
     // case 9 :
     // fmt.Println ( " 9 " )
     // case 10 :
     // fmt.Println ( " 10 " )
     // default: // None of the above conditions are met
     // fmt.Println ( " Don't know " )
     // }

     // 3 multi-conditions
     // a: = 16 
    // switch a {
     // case 1,2,3 :
    // fmt.Println ( " 1 " )
     // case 4,5,6 :
     // fmt.Println ( " 2 " )
     // case 7,9 :
     // fmt.Println ( " 9 " )
     // case 10 , 16 :
     // fmt.Println ( " 10 " )
     // default:
     // fmt.Println ( " don't know " )
     // }

     // 4 no expression
     // a: = 3 
    // switch {
     // case a == 1 || a == 3: // || represents or
    // fmt.Println ( " 1 " )
     // case a == 4 || a == 5 :
     // fmt.Println ( " 2 " )
     // default:
     // fmt.Println ( " don't know " )
     / / }

     // 5 fallthrough, execute the next case unconditionally
     // a: = 3 
    // switch {
     // case a == 1 || a == 3 :
     // fmt.Println ( " 1 " )
     // fallthrough / / fallthrough will execute the next case unconditionally
     // case a == 4 || a == 5 :
     // fmt.Println ( " 2" )
     // default:
     // fmt.Println ( " don't know " )
     // } 
}

 

Guess you like

Origin www.cnblogs.com/baohanblog/p/12747651.html