zoukankan      html  css  js  c++  java
  • Go switch语句

    // switch 是一个条件语句,用于将表达式的值与可能匹配的选项列表进行比较,并根据匹配情况执行相应的代码块。它可以被认为是替代多个 if else 子句的常用方式
    
    
    package main
    
    func main() {
        //1 基本使用
        //num:=4
        //switch num {
        //case 1:
        //    fmt.Println("1")
        //case 2:
        //    fmt.Println("2")
        //case 3:
        //    fmt.Println("3")
        //case 4:
        //    fmt.Println("4")
        //}
    
        //2 默认情况
        //num:=40
        //switch num {
        //case 1:
        //    fmt.Println("1")
        //case 2:
        //    fmt.Println("2")
        //case 3:
        //    fmt.Println("3")
        //case 4:
        //    fmt.Println("4")
        //default:
        //    fmt.Println("我没有匹配")
        //}
    
        //3 多表达式判断
        //num:=40
        //switch num {
        //case 1,2,3,4,5,6,7,8:
        //    fmt.Println("1")
        //case 10,11,16:
        //    fmt.Println("2")
        //case 30:
        //    fmt.Println("3")
        //case 40,44,45:
        //    fmt.Println("4")
        //default:
        //    fmt.Println("我没有匹配")
        //}
    
        //4 无表达式的 switch
        //num:=80
        //switch  {
        //case num==12,num==13:
        //    fmt.Println("12,13")
        //case num==40,num==41:
        //    fmt.Println("40,41")
        //default:
        //    fmt.Println("我没有匹配")
        //}
    
    
        //5 Fallthrough
        //num:=12
        //switch  {
        //case num==12,num==13:
        //    fmt.Println("12,13")
        //    fallthrough
        //case num==40,num==41:
        //    fmt.Println("40,41")
        //    //fallthrough  //穿透,只要看到fallthrough,无条件执行下一个case或者default
        //default:
        //    fmt.Println("我没有匹配")
        //}
    }
    
    
    goto:饱受诟病  java 保留字,没有实际作用
  • 相关阅读:
    hdu 1823 Luck and Love 二维线段树
    UVA 12299 RMQ with Shifts 线段树
    HDU 4578 Transformation 线段树
    FZU 2105 Digits Count 线段树
    UVA 1513 Movie collection 树状数组
    UVA 1292 Strategic game 树形DP
    【ACM】hdu_zs2_1003_Problem C_201308031012
    qsort快速排序
    【ACM】nyoj_7_街区最短路径问题_201308051737
    【ACM】nyoj_540_奇怪的排序_201308050951
  • 原文地址:https://www.cnblogs.com/ZhZhang12138/p/14886715.html
Copyright © 2011-2022 走看看