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 保留字,没有实际作用
  • 相关阅读:
    LCPhash求解
    BSGS
    洛谷—— P1849 [USACO12MAR]拖拉机Tractor
    BZOJ——2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
    洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing
    BZOJ——1601: [Usaco2008 Oct]灌水
    洛谷—— P1342 请柬
    [SDOI2009]Elaxia的路线 SPFA+Topo
    1737 配对
    51Nod 1378 夹克老爷的愤怒
  • 原文地址:https://www.cnblogs.com/ZhZhang12138/p/14886715.html
Copyright © 2011-2022 走看看