if else(分支结构)
go
if score := 65; score >= 90 { //声明并初始化变量 score 为 65
fmt.Println("A")
} else if score > 75 {
fmt.Println("B")
} else {
fmt.Println("C")
}
for(循环结构)
go
for i := 0; i < 10; i++ {
fmt.Println(i)
}
go
for ; i < 10; i++ {
i := 0
fmt.Println(i)
}
for range(键值循环)
go
for i := range 5 {
fmt.Println(i)
}
0
1
2
3
4
switch case
fallthrough语法可以执行满足条件的case的下一个case,是为了兼容C语言中的case设计的。
css
func switchDemo5() {
s := "a"
switch {
case s == "a":
fmt.Println("a")
fallthrough
case s == "b":
fmt.Println("b")
case s == "c":
fmt.Println("c")
default:
fmt.Println("...")
}
}
a
b
goto(跳转到指定标签)
c
if (error_condition) {
goto cleanup;
}
// ...正常逻辑
cleanup:
// 释放资源
c
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (condition) goto loop_exit;
}
}
loop_exit: