条件判断
条件语句需要开发者通过指定一个或多个条件,并通过测试条件是否为 true 来决定是否执行指定语句,并在条件为 false 的情况在执行另外的语句。
下图展示了程序语言中条件语句的结构:
Go 语言提供了以下几种条件判断语句:
语句 | 描述 |
---|---|
if 语句 |
if 语句 由一个布尔表达式后紧跟一个或多个语句组成。 |
if...else 语句 |
if 语句 后可以使用可选的 else 语句, else 语句中的表达式在布尔表达式为 false 时执行。 |
if 嵌套语句 |
你可以在 if 或 else if 语句中嵌入一个或多个 if 或 else if 语句。 |
switch 语句 |
switch 语句用于基于不同条件执行不同动作。 |
select 语句 |
select 语句类似于 switch 语句,但是select会随机执行一个可运行的case。如果没有case可运行,它将阻塞,直到有case可运行。 |
注意:Go 没有三目运算符,所以不支持 ?: 形式的条件判断。
if条件语句
if 语句由布尔表达式后紧跟一个或多个语句组成。
if语句
基本语法
go
if 布尔表达式 {
/* 在布尔表达式为 true 时执行 */
}
Go 完全省略了
if
、switch
和for
结构中条件语句两侧的括号,相比 Java、C++ 和 C# 中减少了很多视觉混乱的因素,同时也使你的代码更加简洁。
If 在布尔表达式为 true 时,其后紧跟的语句块执行,如果为 false 则不执行。
举例
当年龄大于18时输出您已经是个成年人了
go
package control
import "fmt"
func OptionIf(){
//if条件语句测试
var age =19
if age>=18 {
fmt.Println("您已经是个成年人了")
}
}
注意
实际上加上括号也不会有问题,只是在golang的书写规范中if
的条件不要加括号,除非是为了提高优先级
go
package control
import "fmt"
func OptionIf(){
//if条件语句测试
var age =19
//写括号不报错
if (age>=18) {
fmt.Println("您已经是个成年人了")
}
}
if...else语句
if 语句 后可以使用可选的 else 语句, else 语句中的表达式在布尔表达式为 false 时执行。
基本语法
go
if 布尔表达式 {
/* 在布尔表达式为 true 时执行 */
} else {
/* 在布尔表达式为 false 时执行 */
}
If 在布尔表达式为 true 时,其后紧跟的语句块执行,如果为 false 则执行 else 语句块。
流程图如下:
举例
go
package control
import "fmt"
func OptionIf(){
//if...else语句测试
var price = 100
if price<99 {
/* 如果条件为 true 则执行以下语句 */
fmt.Println("I will Buy it")
}else{
/* 如果条件为 false 则执行以下语句 */
fmt.Println("it is so expensive for me that I do not want to buy")
}
}
多层if
基本语法
go
if 布尔表达式1 {
/* 在布尔表达式为 true 时执行 */
} else if 布尔表达式2{
/* 在布尔表达式2为 true 时执行 */
}else{
/* 在以上为 false 时执行 */
}
举例
go
package control
import "fmt"
func OptionIf(){
//if...else if...else
var num = 10
if (num%2)!=0{
fmt.Println("num是奇数")
}else if num<15 {
fmt.Println("num小于15")
}else{
fmt.Println("num是什么")
}
}
结果
sh
num小于15
if嵌套
你可以在 if 或 else if 语句中嵌入一个或多个 if 或 else if 语句。
基本语法
go
if 布尔表达式 1 {
/* 在布尔表达式 1 为 true 时执行 */
if 布尔表达式 2 {
/* 在布尔表达式 2 为 true 时执行 */
}
}
举例
go
package main
import "fmt"
func main() {
/* 定义局部变量 */
var a int = 100
var b int = 200
/* 判断条件 */
if a == 100 {
/* if 条件语句为 true 执行 */
if b == 200 {
/* if 条件语句为 true 执行 */
fmt.Printf("a 的值为 100 , b 的值为 200\n" );
}
}
fmt.Printf("a 值为 : %d\n", a );
fmt.Printf("b 值为 : %d\n", b );
}
结果
sh
a 的值为 100 , b 的值为 200
a 值为 : 100
b 值为 : 200
switch条件语句
switch语句
switch 语句用于基于不同条件执行不同动作,每一个 case 分支都是唯一的,从上至下逐一测试,直到匹配为止。
switch 语句执行的过程从上至下,直到找到匹配项,匹配项后面也不需要再加 break。
switch 默认情况下 case 最后自带 break 语句,匹配成功后就不会执行其他 case,如果我们需要执行后面的 case,可以使用 fallthrough 。
基本语法
go
//语法1,值匹配
switch target {
//等价于 target==value1
case val1:
...
//等价于 target==val1||target==val2||target==val3
case val1,val2,val3:
...
default:
...
}
//语法2条件表达式
switch {
case 条件表达式1:
...
case 条件表达式2:
...
default:
...
}
变量 var1 可以是任何类型,而 val1 和 val2 则可以是同类型的任意值。类型不被局限于常量或整数,但必须是相同的类型;或者最终结果为相同类型的表达式。
您可以同时测试多个可能符合条件的值,使用逗号分割它们,例如:case val1, val2, val3。
解释
本质上是做了一个条件判断,switch目标是否能被case选中。流程图示:
举例
go
package control
import "fmt"
func OptionSwitch(){
var grades string = "A"
marks := 90
// 值匹配,一般用于枚举的场景
switch marks{
case 60: grades = "E"
case 70: grades="D"
case 80:grades="C"
case 90:grades="B"
case 100:grades = "A"
default:
grades="F"
}
//表达式匹配
switch {
case grades=="F":
fmt.Println("其他")
case grades=="E":
fmt.Println("不及格")
case grades=="D":
fmt.Println("一般")
case grades =="C":
fmt.Println("良")
case grades =="B"||grades=="A":
fmt.Println("优")
default:
fmt.Println("其他")
}
fmt.Printf("您的成绩等级为%s",grades)
}
结果
sh
优
您的成绩等级为B
type switch
switch语句的一个常用场景就是为空接口
指定具体类型
在泛型没诞生之前,golang用
interface{}
来接收不确定类型,我们在实际使用变量时需要指明类型。
switch 语句还可以被用于 type-switch 来判断某个 interface 变量中实际存储的变量类型。
基本语法
go
//注意 x的类型为 interface{}
switch x.(type){
case type:
statement(s);
case type:
statement(s);
/* 你可以定义任意个数的case */
default: /* 可选 */
statement(s);
}
举例
go
package main
import "fmt"
func main() {
var x interface{}
switch i := x.(type) {
case nil:
fmt.Printf(" x 的类型 :%T",i)
case int:
fmt.Printf("x 是 int 型")
case float64:
fmt.Printf("x 是 float64 型")
case func(int) float64:
fmt.Printf("x 是 func(int) 型")
case bool, string:
fmt.Printf("x 是 bool 或 string 型" )
default:
fmt.Printf("未知型")
}
}
结果
go
x 的类型 :<nil>
fallthrough
使用 fallthrough 会强制执行后面的 case 语句,fallthrough 不会判断下一条 case 的表达式结果是否为 true。
举例
go
package main
import "fmt"
func main() {
switch {
case false:
fmt.Println("1、case 条件语句为 false")
fallthrough
case true:
fmt.Println("2、case 条件语句为 true")
fallthrough
case false:
fmt.Println("3、case 条件语句为 false")
fallthrough
case true:
fmt.Println("4、case 条件语句为 true")
case false:
fmt.Println("5、case 条件语句为 false")
fallthrough
default:
fmt.Println("6、默认 case")
}
}
结果
go
2、case 条件语句为 true
3、case 条件语句为 false
4、case 条件语句为 true
从以上代码输出的结果可以看出:switch 从第一个判断表达式为 true 的 case 开始执行,如果 case 带有 fallthrough,程序会继续执行下一条 case,且它不会去判断下一个 case 的表达式是否为 true。
拓展
还有一个select结构,等到学习了channel后再了解。