go语言渐入佳境[7]-if判断条件

if条件语句的表现形式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//第一种最基本
num := 26

if(num %2==0){
fmt.Printf("num是偶数\n")
}

 //第二种 初始化:
 if  str:="jonson";  num %2==0 {
 fmt.Printf(str)
}
//第三种 else语句:
if(num %2==0){
fmt.Printf("num是偶数\n")
}else{
fmt.Printf("num是奇数\n")
}

if语句判断学生成绩1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//判断学生成绩

func scoreTest(){

var  score =80

if(score >=90){
fmt.Printf("优秀")
}

if(score >=80 && score <90){
fmt.Printf("良好")
}
if(score >=70 && score <80){
fmt.Printf("中等")
}

if(score >=60 && score <70){
fmt.Printf("及格")
}

if(score <60){
fmt.Printf("不及格")
}
}

if语句判断学生成绩改进2

1
2
3
4
5
6
7
8
9
10
11
12
13
//判断学生成绩
func scoreTest3(score int){
if(score >=90){
fmt.Printf("优秀")
}else if(score >=80){
fmt.Printf("良好")
}else if(score >=70){
fmt.Printf("中等")
}else if(score >=60){
fmt.Printf("及格")
}else{
fmt.Printf("不及格")
}

注意顺序,下面的代码是错误的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(score >=80 && score <90){
fmt.Printf("良好")
}
if(score >=70 && score <80){
fmt.Printf("中等")
}

if(score >=60 && score <70){
fmt.Printf("及格")
}

if(score <60){
fmt.Printf("不及格")
}

switch

switch形式一

使用switch语句判断成绩:

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

import "fmt"

func main(){

score :=56
switch{

case score >=90:
fmt.Printf("优秀")
case score >=80:
fmt.Printf("良好")
case score >=70:
fmt.Printf("中等")
case score >=60:
fmt.Printf("及格")
default:
fmt.Printf("不及格")
}

operate()

}

switch形式二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

func  operate(){
a,b,c := 4,2,0

operate :="*"

switch operate{
case "+":
c=a+b
case "-":
c=a-b
case "*":
c=a*b

case "/":
c=a/b
default:
c = -1
}

fmt.Println(c)
}

switch形式三

判断月份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import "fmt"

func main() {
getDaysByMonth()
}

func getDaysByMonth() {
// 定义局部变量:年、月、日
year := 2008
month := 12
days := 0

switch month {
case 1, 3, 5, 7, 8, 10, 12:
days = 31
case 4, 6, 9, 11:
days = 30
case 2:
   //判断闰年
if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
days = 29
} else {
days = 28
}
default:
days = -1
}
fmt.Printf("%d年%d月的天数为:%d", year , month , days)
}

switch形式四

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

func main() {
eval()
}

func eval() {
num1, num2, result := 12, 4, 0
operation := "+"

switch operation {
case "+":
result = num1 + num2
//fallthrough
case "-":
result = num1 - num2
//fallthrough
case "*":
result = num1 * num2
//fallthrough
case "/":
result = num1 / num2
//fallthrough
case "%":
result = num1 % num2
default:
result = -1
}
fmt.Println(result)
}

总结

1
2
3
4
5
6
7
1、switch 语句执行的过程自上而下,直到找到case匹配项,匹配项中无需使用break,因为Go语言中的switch默认给每个case自带break,因此匹配成功后不会向下执行其他的case分支,而是跳出整个switch。
2、变量 var1 可以是任何类型,而 val1 和 val2 则可以是同类型的任意值。类型不被局限于常量或整数,但必须是相同类型或最终结果为相同类型的表达式。
3、case后的值不能重复。
4、可以同时测试多个符合条件的值,也就是说case后可以有多个值,这些值之间使用逗号分割,例如:case val1, val2, val3。
5、Go语言中switch后的表达式可以省略,那么默认是switch  true。
6、Go语言中的switch case因为自带break,所以匹配某个case后不会自动向下执行其他case,如需贯通后续的case,可以添加fallthrough(中文含义是:贯穿), 强制执行后面的case分支
7、fallthrough必须放在case分支的最后一行。如果它出现在中间的某个地方,编译器就会抛出错误(fallthrough  statement  out  of place,含义是fallthrough不在合适的位置)。

image.png

猜你喜欢

转载自blog.51cto.com/13784902/2326140
今日推荐