zoukankan      html  css  js  c++  java
  • Golang基础——if语句,switch语句

    流程控制

    if 语句:

    a := 10
    if a > 10 {
        fmt.Println("good")
    }
    // 支持初始化语句,用分号分割
    if a := 10;a > 10{
        fmt.Println("good")
    }
    
    if a == 1 {
        fmt.Println("good")
    }else{
        fmt.Println("goodd")
    }
    
    if a == 1{
        fmt.Println(1)
    }else if a ==2 {
        fmt.Println("2")
    }else{
        fmt.Println("else")
    }
    
    // if嵌套
    // if语句:先满足条件的执行,其他互斥的条件都不再判断。
    

    switch语句:

    // 不需要break,从上至下,匹配到一个即结束。
    // 可以匹配多个条件
    // fallthrough 匹配成功后,继续向下执行一个分支
    // 表达式判断
    
    var score = 90
    switch score {
    case 90:
    fmt.Println("A")
    fallthrough
    case 80:
    fmt.Println("B")
    fallthrough
    case 70, 60:
    fmt.Println("good")
    default:
    fmt.Println("C")
    
    switch t := 90; t {
    case 90:
    fmt.Println("A")
    }
        
    t1 := 90
    switch {
    case t1 == 90:
    fmt.Println("AAA")
    }
        
    switch t2 := 90; {
    case t2 == 90:
    fmt.Println("AAA")
    }
    
    // if语句执行效率低;可以嵌套,范围判断。
    // switch 不建议嵌套;固定值判断,多个固定值;效率高。
  • 相关阅读:
    [codevs]失恋28天题目系列
    [NOIP1998]最大数
    [codevs4247]奇特的生物
    [codevs1380]没有上司的舞会
    [codevs2152]滑雪
    [codevs2171]棋盘覆盖
    [codevs2170]悠闲的漫步
    [codevs1557]热浪
    [codevs1554]最佳课题选择
    nodejs建站+github page 建站问题总结
  • 原文地址:https://www.cnblogs.com/pythonwl/p/14508370.html
Copyright © 2011-2022 走看看