go基础语法之流程控制

go基础语法之流程控制

if-else

if语法

1
2
3
4
5
6
7
8
9
if expr1  // 表达式的左括号,一定要在上一行的最后,否则会触发编译错误
	分支1
else if expr2 
	分支2
else expr3 
	分支3

// else if和 else都是可选
// 

demo:

 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 ifDemo1() {
	score := 88

	if score > 90 {
		fmt.Println("good")
	} else if score > 80 {
		fmt.Println("not bad")
	} else {
		fmt.Println("bu za di")
	}
}

func main() {
	ifDemo1()
}

D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
not bad

编译错误demo:

 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
package main

import (
	"fmt"
)

func ifDemo1() {
	score := 88

	if score > 90 
	{ // 要求放在上一行if表达式的后面
		fmt.Println("good")
	} else if score > 80 {
		fmt.Println("not bad")
	} else {
		fmt.Println("bu za di")
	}
}

func main() {
	ifDemo1()
}

D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>go build    
# learngo/basic_grammar/04_controll
.\main.go:10:17: syntax error: unexpected newline, expecting { after if clause 

if特殊语法

在if的表达式之前,加一个执行语句

 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 ifDemo2() {
	
// 把score的表达式放入了if,判断式的前面
	if score := 66;score > 90 {
		fmt.Println("good")
	} else if score > 80 {
		fmt.Println("not bad")
	} else {
		fmt.Println("bu za di")
	}
}

func main() {
	//ifDemo1()
	ifDemo2()
}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
bu za di

for

go中,所有循环,都可以用for实现

for语法

1
2
3
for 初始语句条件表达式结束条件 
	循环体语句

fordemo1:

 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
package main

import (
	"fmt"
)

func fordemo1 (){
	for i :=1 ; i < 10; i++ {
		fmt.Println(i)
	}
}

func main() {
	fordemo1()
}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
1
2
3
4
5
6
7
8
9

fordemo2:省略初始语句放在其他位置

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

import (
	"fmt"
)

func fordemo1 (){
	i := 5
	for ; i < 10; i++ {
		fmt.Println(i)
	}
}

func main() {
	fordemo1()
}

D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
5
6
7
8
9

fordemo3:省略初始语句,和结束条件,放在其他位置;

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

import (
	"fmt"
)

func fordemo1 (){
	i := 5
	for ; i < 10; {
		fmt.Println(i)
		i++
	}
}

func main() {
	fordemo1()
}

fordemo4:无限循环

1
2
3
for {
    循环体
}

for,通过break,goto,return,panic,强制退出循环

for-range键值循环

go中,通过for range,遍历数组、切片、字符串、map、通道channel,for range遍历后返回值特点:

  • 数组、切片、字符串返回索引和值;
  • map返回键和值;
  • 通道channel返回通道内的值;

for-range:demo

 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
package main

import (
	"fmt"
)



func main() {
	s1 := "hello"

	for index, value := range s1 {
		fmt.Printf("%d \n", index)
		fmt.Printf("%c \n", value)
        // 接收索引和字符
	}
}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
0
h
1
e
2
l
3
l
4
o

switch-case

demo1:

 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
package main

import (
	"fmt"
)


func main()  {
	finger := 3

	switch finger {
		case 1:
			fmt.Println("大拇指")
		case 2:
			fmt.Println("食指")
		case 3:
			fmt.Println("中指")
		case 4:
			fmt.Println("无名指")
		case 5:
			fmt.Println("小拇指")
		default:
			fmt.Println("输入无效")
	}
}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
中指

demo2: case比对的值,可以跟有多个

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

import (
	"fmt"
)


func main()  {
	num := 7

	switch num  {
	case 1,3,5,7:
		fmt.Println("奇数")
	case 2,4,6,8:
		fmt.Println("偶数")
	default:
		fmt.Println("自己看")
	}


}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
奇数

demo3: case后跟表达式

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

import (
	"fmt"
)


func main()  {

	score := 100

	switch  { // 此时switch后,不需要跟变量了,每个case后会跟包含变量的表达式
	case score > 90:
		fmt.Println("good")
	case score > 80:
		fmt.Println("not bad")
	default:
		fmt.Println("bad")
	}

}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
good

demo4: fallthrough语法,可以执行满足条件的case的下一个case,为了兼容c语言中的case设计;

 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
package main

import (
	"fmt"
)

func main() {

	score := 100

	switch {
	case score > 90:
		fmt.Println("good")
		fallthrough
        // 输出good后,会输出下一个not bad
	case score > 80:
		fmt.Println("not bad")
	default:
		fmt.Println("bad")
	}

}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
good
not bad

goto

goto语句,通过标签,进行代码之间的无条件跳转;也可方便简化代码

1,跳出双层for循环:(没用goto

 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
package main

import (
	"fmt"
)

// goto demo1

func main() {
	var breakFlag bool

	for i := 0; i < 10; i++ {
		for j := 0; j < 10; j++ {
			if j == 2 {
				breakFlag = true
				break // 先跳出内层循环
			}
			fmt.Printf("%v-%v\n", i, j)
		}

		if breakFlag { // 再根据内层循环设置的标记位,跳出外层循环
			break
		}
	}
}

D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
0-0
0-1

2,简化代码:(用了goto

 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
package main

import (
	"fmt"
)
// goto demo2
func main()  {
	for i := 0; i < 10; i++ {
		for j := 0; j < 10; j ++ {
			if j == 2 {
				goto breakTag // 直接跳转到2个for循环之外的设置的标记位:breakTag,一次跳出2个循环
			}
			fmt.Printf("%v-%v\n", i,j)
		}
	}

breakTag:
	fmt.Println("结束for循环")	

}

D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
0-0
0-1
结束for循环

break跳出本层循环

break可以结束for、switch、select的代码块:

break还可以在语句后加标签,表示退出某个标签对应的代码块:

标签必须定义在对应的for,switch,select的代码块上:

借助标签,跳出2层循环

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

import "fmt"

// break demo1

func main() {
breakdemo1:
	for i := 1; i < 10; i++ {
		for j := 1; j < 10; j++ {
			if j == 2 {
				break breakdemo1
			}

			fmt.Printf("%v-%v", i, j)
		}
	}

	fmt.Println("...")
}

D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
1-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
26
27
28
29
30
package main

import "fmt"

// break demo1

func main() {
	for i := 1; i < 10; i++ {
		for j := 1; j < 10; j++ {
			if j == 2 {
				break
			}

			fmt.Printf("%v-%v\n", i, j)
		}
	}

	fmt.Println("...")
}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
1-1
2-1
3-1
4-1
5-1
6-1
7-1
8-1
9-1
...

continue跳出本次循环继续本层的下次循环

continue语句,结束本次循环,进入本层循环的下一次循环;

仅仅在for循环内部使用

continue语句后加标签,表示开始标签对应的循环

借助标签,可以跨越2层循环

 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
package main

import "fmt"

// continue demo1
func main() {
forloop1:
	for i := 0; i < 5; i++ {
		for j := 0; j < 5; j++ {
			if i == 2 && j == 2 {
				continue forloop1
			}

			fmt.Printf("%v-%v\n", i, j)
		}
	}
}
D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
0-0
0-1
0-2
0-3
0-4
1-0
1-1
1-2
1-3
1-4
2-0
2-1
避开了2-2,2-3,2-4
3-0
3-1
3-2
3-3
3-4
4-0
4-1
4-2
4-3
4-4

没有借助标签,只能跳出一层循环,只跳出了本层循环的本次循环

 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
package main

import "fmt"

// continue demo1
func main() {

	for i := 0; i < 5; i++ {
		for j := 0; j < 5; j++ {
			if i == 2 && j == 2 {
				continue 
			}

			fmt.Printf("%v-%v\n", i, j)
		}
	}
}


    

D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>04_controll.exe
0-0
0-1
0-2
0-3
0-4
1-0
1-1
1-2
1-3
1-4
2-0
2-1
只避开了2-2
2-3
2-4
3-0
3-1
3-2
3-3
3-4
4-0
4-1
4-2
4-3
4-4

练习题

  1. 编写代码打印9*9乘法表

     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
    
    package main
       
    import "fmt"
       
    // 9x9 乘法表打印 
       
    func main(){
     for i := 1; i <=9; i++ {
         for j := 1; j <=i; j++ {
             fmt.Printf("%v * %v = %v\t", j,i,j*i)
         }
         fmt.Println()
     }
    }
       
    D:\workstation\mycode\gocode\src\learngo\basic_grammar\04_controll>go run main.go
    1 * 1 = 1
    1 * 2 = 2       2 * 2 = 4
    1 * 3 = 3       2 * 3 = 6       3 * 3 = 9
    1 * 4 = 4       2 * 4 = 8       3 * 4 = 12      4 * 4 = 16
    1 * 5 = 5       2 * 5 = 10      3 * 5 = 15      4 * 5 = 20      5 * 5 = 25
    1 * 6 = 6       2 * 6 = 12      3 * 6 = 18      4 * 6 = 24      5 * 6 = 30      6 * 6 = 36
    1 * 7 = 7       2 * 7 = 14      3 * 7 = 21      4 * 7 = 28      5 * 7 = 35      6 * 7 = 42      7 * 7 = 49
    1 * 8 = 8       2 * 8 = 16      3 * 8 = 24      4 * 8 = 32      5 * 8 = 40      6 * 8 = 48      7 * 8 = 56      8 * 8 = 64
    1 * 9 = 9       2 * 9 = 18      3 * 9 = 27      4 * 9 = 36      5 * 9 = 45      6 * 9 = 54      7 * 9 = 63      8 * 9 = 72      9 * 9 = 81
    
updatedupdated2020-12-312020-12-31
加载评论