Go语言的条件语句

一、if语句

基本语法:

if 条件表达式 {
	执行语句块
}

说明:当条件表达式为true时,就会执行{}里的代码
注意:{}时必须有的,不能省略,否则编译不通过

执行流程图如下所示:
image

使用细节:if还有一个强大的地方就是条件表达式那里允许声明一个变量,这个变量的作用域只在该条件逻辑块内,其他地方不起作用。

例子:

package main

import "fmt"

func main() {
	if age := 10; age > 9 {
		fmt.Println("Hello, World!")
	} 
}

二、if-else语句

基本语法

if 条件表达式 {
	执行代码块1
} else {
	执行代码块2
}

说明:当条件表达式为true时,执行代码块1,否则执行代码块2
执行流程图如下所示:
image

例子:

package main

import "fmt"

func main() {
	if age := 19; age > 18 {
		fmt.Println("年龄大于18岁")
	} else {
		fmt.Println("年龄小于18岁")
	}  
}

三、if-else if-...=else

基本语法:

if 条件表达式1 {
	执行代码块1
} else if 条件表达式2 {
	执行代码块2
}
...
esle {
	执行代码块n
}

说明:先判断表达式1,若为true则执行代码块1,否则判断表达2,若表达式2位true,则执行代码块2,否则以此类推,若所有表达式都不成立,则执行代码块n。

使用细节:

  1. else不是必须的
  2. 多分支只能有一个执行入口

执行流程图如下:
image

package main

import "fmt"

func main() {
	if age := 19; age > 18 {
		fmt.Println("年龄大于18岁")
	} else if  age > 15{
		fmt.Println("年龄大于15岁")
	} else if age > 10 {
		fmt.Println("年龄大于10岁")
	} else {
		fmt.Println("年龄太小")
	}
}

输出结果:

年龄大于18岁

四、swich语句

基本语法

switch 表达式 {
    case 表达式1,表达式2,...:
        代码块1
    case 表达式3,表达式4,...:
        代码块2
	case 表达式4,表达式5,...:
        代码块3
    default:
        代码块n
}

说明:switch 语句用于基于不同条件执行不同动作,每一个 case 分支都是唯一的,从上至下逐一测试,直到匹配为止。

使用细节:

  1. case后是一个表达式(即:常量值,变量,一个有返回值得函数等都可以)
  2. case后的各个表达式的值得数据类型,必须和switch的表达式的数据类型一致
  3. case后的表达式可以为多个,使用逗号分隔。
  4. case后面的表达式如果是常量值,则要求不能重复
  5. switch 默认情况下 case 最后自带 break 语句,匹配成功后就不会执行其他 case,如果一个都匹配不到则执行default,如果我们需要执行后面的case,可以使用fallthrough
package main

import "fmt"

func main() {
	var mouth byte = 5	
	switch mouth {
	case 1,2,3:
		fmt.Println("该月是第一季度")
	case 4,5,6 :
		fmt.Println("该月是第二季度")
		fallthrough // 默认只能穿透一层
	case 7,8,9 :
		fmt.Println("该月是第三季度")
	case 10,11,12 :
		fmt.Println("该月是第四季度")
	}
}

输出结果

该月是第二季度
该月是第三季度
  1. default语句是不必须的
  2. switch后可以不带表达式,类似if-else分支来使用
package main

import "fmt"

func main() {
	var mouth byte = 8	
	switch {
	case 1 <= mouth &&  mouth <4:
		fmt.Println("该月是第一季度")
	case 4 <= mouth &&  mouth < 7 :
		fmt.Println("该月是第二季度")
	case 7 <= mouth &&  mouth < 10 :
		fmt.Println("该月是第三季度")
	case 10 <= mouth &&  mouth <= 12 :
		fmt.Println("该月是第四季度")
	}
}

输出结果:

该月是第三季度
  1. switch后也可以声明/定义变量,分号结束
package main

import "fmt"

func main() {	
	switch mouth := 10; {
	case 1 <= mouth &&  mouth <4:
		fmt.Println("该月是第一季度")
	case 4 <= mouth &&  mouth < 7 :
		fmt.Println("该月是第二季度")
	case 7 <= mouth &&  mouth < 10 :
		fmt.Println("该月是第三季度")
	case 10 <= mouth &&  mouth <= 12 :
		fmt.Println("该月是第四季度")
	}
}

输出结果

该月是第四季度

9.Type-switch:switch语句还可以被用于type-switch来判断某个interfance变量中实际指向的变量类型

package main

import "fmt"

func main() {
	var x interface{}
	var y = 10.0
	x = y
	switch xType := x.(type) {
	case nil:
		fmt.Printf("x的类型:%T\n",xType)
	case int:
		fmt.Println("x是int类型")
	case float32:
		fmt.Println("x是float32类型")
	case float64:
		fmt.Println("x是float64类型")
	case bool:
		fmt.Println("x是bool类型")
	case string:
		fmt.Println("x是string类型")
	default:
		fmt.Println("未知类型")
	}
}

输出结果

x是float64类型

执行流程图:
image

package main

import "fmt"

func main() {
	var mouth byte = 8	
	switch mouth {
	case 1,2,3 :
		fmt.Println("第一季度")
	case 4,5,6 :
		fmt.Println("第二季度")
	case 7,8,9 :
		fmt.Println("第三季度")
	case 10,11,12 :
		fmt.Println("第四季度")
	}
}

输出结果:

第三季度
posted @ 2021-05-06 22:33  若雨蚂蚱  阅读(122)  评论(0编辑  收藏  举报