zoukankan      html  css  js  c++  java
  • 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("不知道")
    	//}
    }
    
    
    
    
  • 相关阅读:
    ASP.NET MVC中获取URL地址参数的两种写法
    SQL Server之存储过程基础知识
    ASP.NET MVC 四种Controller向View传值方法
    Js数据类型、Json格式、Json对象、Json字符串
    调用微信内置的方法及wx.config的配置问题
    ref和out的使用及区别
    ASP.NET MVC post请求接收参数的三种方式
    Asp.Net Mvc 路由机制
    Asp.Net MVC中Action跳转小结
    JS应用MD5散列计算头像URL
  • 原文地址:https://www.cnblogs.com/kai-/p/12747378.html
Copyright © 2011-2022 走看看