continue(继续下一次循环)
# continue(继续下一次循环)
Go语言中 continue 语句可以结束当前循环,开始下一次的循环迭代过程,仅限在 for 循环内使用,在 continue 语句后添加标签时,表示开始标签对应的循环
# 基本使用
package main
import "fmt"
func main() {
// 一、基本使用
for n := 1; n <= 9; n++ {
if n == 6 {
continue
}
fmt.Printf("%d \t", n)
}
// 1 2 3 4 5 7 8 9
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 标签的使用
- 标签后的语句都不执行
package main
import "fmt"
func main() {
labelTest:
for n1 := 1; n1 <= 9; n1++ {
for j := 1; j <= n1; j++ {
if n1 == 6 {
// continue
continue labelTest
}
fmt.Printf("%d x %d = %d\t", n1, j, n1*j)
}
fmt.Println(11111111)
}
/*
# 未添加标签
1 x 1 = 1 11111111
2 x 1 = 2 2 x 2 = 4 11111111
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 11111111
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 11111111
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 11111111
11111111
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 11111111
8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 11111111
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 11111111
添加后,后面的语句不执行
1 x 1 = 1 11111111
2 x 1 = 2 2 x 2 = 4 11111111
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 11111111
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 11111111
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 11111111
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 11111111
8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 11111111
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 11111111
*/
}
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
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
编辑 (opens new window)