文章

golang学习记录(6)

条件判断和for循环

1、if条件判断

1.1、if条件判断的基本语法

个其他语言差不多,go中if语句后面的条件判断可以加括号,也可以不加,通常不写括号,但是如果条件比较复杂,建议加上括号。

1
2
3
4
5
6
7
8
9
age := 10
//1、加括号
if age > 18 {
    fmt.Println("你已经成年了")
}
//2、不加括号
if (age > 18){
    fmt.Println("你已经成年了")
}

1.2、if else语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
age := 10
if age > 18 {
    fmt.Println("你已经成年了")
} else {
    fmt.Println("你还没有成年")
}

// else if
if age > 20 {
    fmt.Println("你已经成年了")
} else if a18 > 16 {
    fmt.Println("你已经成年了")
} else {
    fmt.Println("你还没有成年")
}

2、for循环

go中没有while循环,只有for循环

2.1、for循环的基本语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//基础写法
for i := 0; i < 10; i++ {
    fmt.Println(i)
}
//注意不能加括号

//for循环代替while循环
var i int = 0
for {
    if i > 10 {
        break
    }
    fmt.Println(i)
    i++
}

2.2、for循环使用举例

2.2.1、使用for循环打印1-100之间的所有偶数

1
2
3
4
5
for i := 1; i <= 1--; i++{
    if i % 2 == 0{
        fmt.Println(i)
    }
}

2.2.2、使用for循环打印九九乘法表

1
2
3
4
5
6
for i := 1; i <= 9; i++{
    for j := 1; j <= i; j++{
        fmt.Printf("%d * %d = %d\t", j, i, i * j)
    }
    fmt.Println()
}

2.3、for range介绍和使用

for range 主要对于数组、切片、字符串、map、通道等进行迭代循环,返回的是索引和值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
name := "hello go, hello world"

for index, value := range name {
    fmt.Printf("index = %d, value = %c\n", index, value)
}

for _, value := range name {
    fmt.Printf("value = %c\n", value)
}

for index := range name {
    fmt.Printf("index = %d\n", index)
    fmt.Printf("value = %c\n", name[index])
}

注意: go

2.4、for循环的退出

使用breakcontinue来退出循环。

continue 用于跳过当前循环的剩余语句,然后继续执行下一次循环。

break 用于跳出当前循环,执行循环之后的语句。

3、goto语句

goto语句可以让我们的代码跳转到指定的代码块中运行,但是不建议使用goto语句,因为goto语句会让代码的可读性变差,而且也会让代码的维护变得困难。

go语言中的goto语句可以实现程序的跳转,goto语句使用最多的是程序的错误处理,当程序处理错误时,跳转到相应的标签处统一处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
for i := 0; i < 10; i++ {
    for j := 0; j < 10; j++ {
        if j == 5 {
            goto over
        }
        fmt.Println(i, j)
    }
}

//任意代码块
over:
    fmt.Println("over")
//代码直接跳转到over代码块中运行

4、switch语句

switch语句用于根据不同的条件执行不同的代码块,它可以替代多个if else语句。

提升代码的可读性

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//基础写法
switch var1{
    case val1:
       ...
    case val2:
       ...
    default:
       ...
}

//举例--中文的星期几,输出对应的英文
day := "星期一"

switch dat{
    case "星期一":
        fmt.Println("Monday")
    case "星期二":
        fmt.Println("Tuesday")
    case "星期三":
        fmt.Println("Wednesday")
    case "星期四":
        fmt.Println("Thursday")
    case "星期五":
        fmt.Println("Friday")
    case "星期六":
        fmt.Println("Saturday")
    case "星期日":
        fmt.Println("Sunday")
    default:
        fmt.Println("Unknown")
}

siwtch {
    case day == "星期一":
        fmt.Println("Monday")
    case day == "星期二":
        fmt.Println("Tuesday")
    case day == "星期三":
        fmt.Println("Wednesday")
    case day == "星期四":
        fmt.Println("Thursday")
    case day == "星期五":
        fmt.Println("Friday")
    case day == "星期六":
        fmt.Println("Saturday")
    case day == "星期日":
        fmt.Println("Sunday")
    default:
        fmt.Println("Unknown")
}

score := 90

switch {
    case score >= 90:
        fmt.Println("A")
    case score >= 80:
        fmt.Println("B")
    case score >= 70:
        fmt.Println("C")
    case score >= 60:
        fmt.Println("D")
    default:
        fmt.Println("E")
}

注意:Go语言的switch语句不需要break关键字来终止每个case。如果不使用break,执行完一个case后,程序会自动退出switch语句。

本文由作者按照 CC BY 4.0 进行授权