• if-else 语句 switch 语句


    if-else 语句

    if 是条件语句。if 语句的语法是

    if condition {  
    }
    

    如果 condition 为真,则执行 {} 之间的代码。

    不同于其他语言,例如 C 语言,Go 语言里的 { } 是必要的,即使在 { } 之间只有一条语句。

    if 语句还有可选的 else ifelse 部分。

    if condition {  
    } else if condition {
    } else {
    }
    

    if-else 语句之间可以有任意数量的 else if。条件判断顺序是从上到下。如果 ifelse if 条件判断的结果为真,则执行相应的代码块。 如果没有条件为真,则 else 代码块被执行。

    让我们编写一个简单的程序来检测一个数字是奇数还是偶数

    //if-else
    package main
    
    func main() {
    	//a:=9
    	//1 基本使用
    	//if a>10{
    	//	fmt.Println("大于10")
    	//}else {
    	//	fmt.Println("小于等于10")
    	//}
    
    	//2 if -else if -else
    	//if a>10{
    	//	fmt.Println("大于10")
    	//}else if a==10 {
    	//	fmt.Println("等于10")
    	//}else {
    	//	fmt.Println("小于10")
    	//}
    
    	//3 不能换行(go语言每一行结尾,需要加一个;  ,每当换行,会自动加;)
    	//if a>10{
    	//	fmt.Println("大于10")
    	//}else if a==10 {
    	//	fmt.Println("等于10")
    	//}else
    	//{
    	//	fmt.Println("小于10")
    	//}
    
    	//4 条件里可以进行初始化操作(有作用域范围的区别)
    	//a:=10;
    	//if a<10{
    	//if a:=10;a<10{
    	//	fmt.Println("xxx")
    	//}else {
    	//	fmt.Println("yyyy")
    	//}
    	//fmt.Println(a)
    
    	//fmt.Println(a)
    }
    

    switch 语句

    switch 是一个条件语句,用于将表达式的值与可能匹配的选项列表进行比较,并根据匹配情况执行相应的代码块。它可以被认为是替代多个 if else 子句的常用方式。

    看代码比文字更容易理解。让我们从一个简单的例子开始,它将把一个手指的编号作为输入,然后输出该手指对应的名字。比如 0 是拇指,1 是食指等等。

    //switch
    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:
    	//	fmt.Println("不知道")
    	//}
    
    	//3 多条件
    	//a:=3
    	//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("不知道")
    	//}
    
    
    	//4 无表达式
    	//a:=3
    	//switch  {
    	//case a==1 || a==3:
    	//	fmt.Println("1")
    	//case a==4||a==5:
    	//	fmt.Println(2)
    	//default:
    	//	fmt.Println("不知道")
    	//}
    
    	//5 fallthrough,无条件执行下一个case
    	//a:=1
    	//switch  {
    	//case a==1 || a==3:
    	//	fmt.Println("1")
    	//	//fallthrough  //fallthrough 会无条件执行下一个case
    	//case a==4||a==5:
    	//	fmt.Println(2)
    	//	fallthrough
    	//default:
    	//	fmt.Println("不知道")
    	//}
    }
    
    
    
    
  • 相关阅读:
    js复制文字到剪切板
    js推送网页到扩展屏上
    数据库自增ID用完了会怎么样?
    使用@nuxtjs/sitemap给项目添加sitemap(网站地图)
    js实现视频截图,视频批量截图,canvas实现
    javascript深入参数传递
    基于JQ使用原生js构造一个自动回复随机消息的机器人
    把nodejs程序打包成可执行文件
    如何巧妙使用some函数来优化性能
    Eggjs 设置跨域请求 指定地址跨域 nodejs
  • 原文地址:https://www.cnblogs.com/kai-/p/12747378.html
Copyright © 2020-2023  润新知