golang_listen4_流程控制

1.if else语句块

2. for语句 

3. switch语句

 

Go里面switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行后面的case代码,fallthrough不会判断下一条case的expr结果是否为true

基本语法

 if condition{

//do something }

 

if condition { //do something

} else if condition { //do something

} else { //do something

}

func testFor2() {
    var i int
    for i = 1; i <= 10; i++ {
        if i == 5 {
            break
        }
        fmt.Printf("%d is  是4\n", i)
    }
    fmt.Printf("last num is是5 %d\n", i)
}

 

 

 ---------------

 

 -------------

Go语⾔中只有⼀种循环 for

for initialisation; condition; post { }

 

 

 

-------------

 

 ------------------

 

-------------------

 

 ----------------------------

 

 

---------------------------

 

 

--------------------

 

 

func testSwitchV3() {
    
    switch a := getValue(); a {
    case 1,2,3,4,5:
        fmt.Printf("a>=1 and a <= 5\n")
    case 6,7,8,9,10:
        fmt.Printf("a >= 6 and a <= 10\n")
    default:
        fmt.Printf("a > 10\n")
    }
    
}


func testSwitchV4() {
    
    var num = 102
    switch  {
    case num >=0 && num <= 25:
        fmt.Printf("a>=0 and a <= 25\n")
    case num > 25 && num <= 50:
        fmt.Printf("a >25 and a <= 50\n")
    case num >50 && num <= 75:
        fmt.Printf("a >50 and a <= 75\n")
    case num> 75&&num <= 100:
        fmt.Printf("a >75 and a <= 100\n")
    default:
        fmt.Printf("invalid num=%d\n", num)
    }
    
}


func testSwitchV5() {
    
    var num = 60
    switch  {
    case num >=0 && num <= 25:
        fmt.Printf("a>=0 and a <= 25\n")
    case num > 25 && num <= 50:
        fmt.Printf("a >25 and a <= 50\n")
    case num >50 && num <= 75:
        fmt.Printf("a >50 and a <= 75\n")
        fmt.Printf("a=%d\n", num)
        fallthrough
    case num> 75&&num <= 100:
        fmt.Printf("a >75 and a <= 100\n")
    default:
        fmt.Printf("invalid num=%d\n", num)
    }
    
}

 

------------------------------------

 

 

----------------------------

 

posted @ 2022-03-28 21:02  walkerpython  阅读(29)  评论(0编辑  收藏  举报