Skip to content

Commit 16f4096

Browse files
committed
update lesson6 and lesson7
1 parent a1b9d02 commit 16f4096

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

workspace/lesson6/switch.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ func main() {
3232
result = "exception"
3333
}
3434
println("gender:", gender, "result:", result) // the value of result is "better"
35+
36+
37+
switch {
38+
case grade >= 90:
39+
level = "A"
40+
case grade >= 80 && grade < 90:
41+
level = "B"
42+
case grade >= 60 && grade <80:
43+
level = "C"
44+
case grade <60:
45+
level = "D"
46+
}
47+
println("grade:", grade, "level:", level)
3548
}

workspace/lesson7/control-each.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
func main() {
4+
str := "abcdefg"
5+
for index, char := range str {
6+
println("index=", index, "char=", char)
7+
}
8+
9+
numbers := [6]int{1, 2, 3, 5} // size为6的数组,第5个和第6个元素的值是默认值0
10+
for index, value := range numbers {
11+
println("index=", index, "value=", value)
12+
}
13+
14+
strings := []string{"google", "nb"} // 2个元素的字符串数组
15+
for index, value := range strings {
16+
println("index=", index, "value=", value)
17+
}
18+
}

workspace/lesson7/control.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
const num int = 10
4+
5+
func main() {
6+
var sum = 0
7+
for i:=1; i<=num; i++ {
8+
sum +=i
9+
}
10+
11+
var sum2 = 0
12+
var j int = 0
13+
for j<=10 {
14+
sum2 += j
15+
j++
16+
}
17+
println("sum2=", sum2)
18+
19+
for {
20+
println("test infinite loop")
21+
break
22+
}
23+
24+
for true {
25+
println("test infinite loop2")
26+
break
27+
}
28+
}

workspace/lesson7/goto.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
LOOP:
7+
println("Enter your age:")
8+
var age int
9+
_, err := fmt.Scan(&age) // 接受控制台输入
10+
if err != nil {
11+
println("error:", err)
12+
goto LOOP
13+
}
14+
if age < 18 {
15+
println("You are not eligible to vote!")
16+
goto LOOP
17+
} else {
18+
println("You are eligible to vote!")
19+
}
20+
println("all finish")
21+
}

workspace/lesson7/readme.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# f循环控制
2+
3+
* for的4种用法
4+
5+
* 用法1: 类似C++的for(int i=0; i<100; i++)
6+
7+
```go
8+
for init; condition; post {
9+
do sth
10+
}
11+
for ; condition; { // 类似用法2
12+
do sth
13+
}
14+
```
15+
16+
* 用法2:类似C++的while循环
17+
18+
```go
19+
for condition {
20+
do sth
21+
}
22+
```
23+
24+
* 用法3: 死循环,类似C++的for(;;)
25+
26+
```go
27+
for {
28+
do sth
29+
}
30+
```
31+
32+
* 用法4: For-each range循环, 类似python的 for k,v in dict.items()
33+
34+
可以对slice,map,数组和字符串等数据类型进行For-each迭代循环
35+
36+
```go
37+
for key, value := range map1 { // 遍历map
38+
do sth
39+
}
40+
for index, value := range list { // 遍历数组
41+
do sth
42+
}
43+
for index, character := range str { // 遍历字符串
44+
do sth
45+
}
46+
```
47+
48+
* break:跳出for循环或者switch控制逻辑
49+
50+
* continue:结束当前循环,继续下一轮循环
51+
52+
* goto:类似C++里的goto
53+
54+
* 语法
55+
56+
```go
57+
label: statement
58+
goto label
59+
```
60+
61+
* 代码示例
62+
63+
```go
64+
package main
65+
66+
import "fmt"
67+
68+
func main() {
69+
LOOP:
70+
println("Enter your age:")
71+
var age int
72+
_, err := fmt.Scan(&age) // 接受控制台输入
73+
if err != nil {
74+
println("error:", err)
75+
goto LOOP
76+
}
77+
if age < 18 {
78+
println("You are not eligible to vote!")
79+
goto LOOP
80+
} else {
81+
println("You are eligible to vote!")
82+
}
83+
println("all finish")
84+
}
85+
```
86+
87+
88+

0 commit comments

Comments
 (0)