Go语言入门04——条件语句

本文最后更新于 2024年10月15日 下午

条件语句就是根据不同的条件执行不同的代码。

示意图:

if语句

if语句用于判断某个条件是否满足,当条件满足时则执行if语句块中的代码。

语法:

1
2
3
if 条件表达式 {
// 当条件表达式为true时执行
}

代码示例:

1
2
3
4
5
6
7
8
9
10
package main

import "fmt"

func main() {
score := 90
if score >= 90 {
fmt.Println("优秀")
}
}

因为表达式score >= 90的结果为true,则会在控制台打印出优秀

运行结果:

if…else

在if条件语句中,如果表达式为true则会执行if语句块中的代码,在if后还可以增加else语句块,则表示当if条件为false时执行else中的代码。

语法:

1
2
3
4
5
if 条件表达式 {
// 当条件表达式为true时执行
} else {
// 当条件表达式为false时执行
}

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
package main

import "fmt"

func main() {
score := 80
if score >= 90 {
fmt.Println("优秀")
} else {
fmt.Println("一般")
}
}

score = 80,由于表达式score >= 90结果为false,则不会执行if中的语句块,会转而执行else中的语句块。

运行结果:

除了直接使用if...else之外,还可以在else后面继续增加if条件判断。

语法:

1
2
3
4
5
6
7
if 条件表达式1 {
// 当条件表达式1为true时执行
} else if 条件表达式2 {
// 当条件表达式2为true时执行
} else {
// 当条件表达式1和2都不为true时执行
}

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import "fmt"

func main() {
score := 80
if score >= 90 {
fmt.Println("优秀")
} else if score >= 80 {
fmt.Println("良好")
} else {
fmt.Println("一般")
}
}

运行结果:

if...else if判断没有数量限制,可一直添加else if判断,但是在实际开发中不建议写太多,如果有特别多得条件需要判断,可使用switch进行判断

if嵌套

if嵌套表示可以在if语句块中添加if判断

语法:

1
2
3
4
5
6
7
8
9
if 条件表达式1 {
if 条件表达式2 {
// 当条件表达式2为true时执行
} else {
// 当条件表达式2为false时执行
}
} else {
// 当条件表达式1为false时执行
}

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import "fmt"

func main() {
score := 90
if score >= 90 {
if score >= 95 {
fmt.Println("非常优秀")
} else {
fmt.Println("优秀")
}
} else {
fmt.Println("一般")
}
}

score = 90,则第一层if判断score >= 90为true,则进入到第一层if语句块中,在第一层语句块中score >= 95为false,则执行else中的代码,最后输出为优秀。

运行结果:

if嵌套也可以无限制的嵌套,但是在实际开发中同样不建议嵌套太多层。

switch

switch语句是根据变量的值执行不同的case,在执行的过程中,会从第一个case开始判断,直到碰到一个符合条件的case为止,然后执行该case中的语句,不同于java的是不需要在每一个case中添加break语句,go语言默认情况下case后面自带break语句。

语法:

1
2
3
4
5
6
7
8
switch 变量 {
case 变量1:
// 当变量和变量1相等时执行
case 变量2:
// 当变量和变量2相等时执行
default:
// 当没有符合的case时执行
}

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

import "fmt"

func main() {
score := 80
switch score {
case 90:fmt.Println("优秀")
case 80:fmt.Println("良好")
case 70:fmt.Println("一般")
default:fmt.Println("默认")
}
}

运行结果:

switch的变量可以时任意类型,而case的变量可以是相同类型的任意值,类型不局限,但是switch和case的类型必须时相同的类型,同样case还可以为表达式。

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

import "fmt"

func main() {
score := 80
switch {
case score >= 90:fmt.Println("优秀")
case score >= 80:fmt.Println("良好")
case score >= 70:fmt.Println("一般")
default:fmt.Println("默认")
}
}

当case为表达式时,则switch中就不需要再写变量。

运行结果:

fallthrough

由于在go语言中每一个case后面都会默认加上break,所以每次匹配都执行其中的一个case,但是如果需要执行后面的case,则可以使用fallthrough,使用 fallthrough 会强制执行后面的 case 语句,fallthrough 不会判断下一条 case 的表达式结果是否为 true。

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main

import "fmt"

func main() {
score := 80
switch {
case score >= 90:
fmt.Println("优秀")
fallthrough
case score >= 80:
fmt.Println("良好")
fallthrough
case score >= 70:
fmt.Println("一般")
fallthrough
default:fmt.Println("默认")
}
}

由于在每个case后面都增加了fallthrough,则当case score >= 80匹配时,除了执行该case中的语句外,还会直接之后它后面的所有case,并且不会判断case是否为true。

运行结果:


Go语言入门04——条件语句
http://example.com/p/3f78afe9.html
作者
jrlee
发布于
2024年9月22日
许可协议