目录:
一、判断
二、循环
二、复合逻辑
一、判断
-
if...else...
嵌套
func main() {
age :=18
if age>0{
if age<100{
fmt.Printf("%d?this's ok",age)
}
}else {
fmt.Println("Are u kidding me?")
}
}
#输出
18?this's ok
- switch 语句
func main() {
var grade string
marks := 90
switch marks {
case 90:
grade = "A"
case 80:
grade = "B"
case 50,60,70:
grade = "C"
default:
grade = "D"
}
fmt.Printf("grade is %s",grade)
}
#输出
grade is A
如果使用 fallthrough 会强制执行后面的 case 语句,fallthrough 不会判断下一条 case 的表达式结果是否为 true,但是前提是成功执行当前语句,做个对比。
func main() {
var grade string
marks := 90
switch marks {
case 90:
grade = "A"
fmt.Printf("grade is %s",grade)
case 80:
grade = "B"
fmt.Printf("grade is %s",grade)
fallthrough
case 50,60,70:
grade = "C"
fmt.Printf("grade is %s",grade)
default:
grade = "D"
fmt.Printf("grade is %s",grade)
}
#输出
grade is A
func main() {
var grade string
marks := 90
switch marks {
case 90:
grade = "A"
fmt.Printf("grade is %s\n",grade)
fallthrough
case 80:
grade = "B"
fmt.Printf("grade is %s\n",grade)
case 50,60,70:
grade = "C"
fmt.Printf("grade is %s\n",grade)
default:
grade = "D"
fmt.Printf("grade is %s\n",grade)
}
}
#输出
grade is A
grade is B
二、循环
这里有个和其它语言较大带的差异点,go语言没有while语法。
for init; condition; post { }
init: 一般为赋值表达式,给控制变量赋初值;
condition: 关系表达式或逻辑表达式,循环控制条件;
post: 一般为赋值表达式,给控制变量增量或减量。
类似C语言的最基本使用方式,我们以小九九为例:
func main() {
var i, j int
for i = 1; i < 10; i++ {
for j = 1; j < 10; j++ {
fmt.Printf("%d X %d = %d ", i, j, i*j)
}
fmt.Print("\n")
}
}
#输出
小九九(略)
-
for condition { }
init参数和post参数是可以省略的,我们刚刚提到了,Go语言没有while语法,for condition { }
的语法习惯就类似于C语言中的while,同样类似C语言的while(1)
可以使用for ture
代替。
标准循环方式:
func main() {
i :=1
for i<=3 {
fmt.Println(i)
i++
}
}
#输出
1
2
3
无限循环方式:
func main() {
i :=1
for true {
fmt.Println(i)
i++
}
}
或者再次简化:
func main() {
i :=1
for {
fmt.Println(i)
i++
}
}
三、复合逻辑
在很多实际情况下,循环和判断是结合在一起的,借助判断对循环进行逻辑控制,像其他语言一样,Go语言保留了break和continue语句,另外保留了类似C的goto语句,即无条件转移语句。除去基本功能外,三种语句均支持标签跳转。
前排小科普,其他语言也会有goto,但不一定生效。如在java中,goto确实是保留字,但goto并未启用,官方也并未打算启用它,因为java中的break和continue一定程度上可以替代它,所以你完全可以理解成java不支持goto语句,其他语言类似,此处不做展开讨论。
-
break
我们依然以小九九为例,把对其进行限制:
func main() {
var i, j int
for i = 1; i < 10; i++ {
for j = 1; j < 10; j++ {
if i>6 || j>6 {
break;
}
fmt.Printf("%d X %d = %d ", i, j, i*j)
}
fmt.Print("\n")
}
}
#输出
1 X 1 = 1 1 X 2 = 2 1 X 3 = 3 1 X 4 = 4 1 X 5 = 5 1 X 6 = 6
2 X 1 = 2 2 X 2 = 4 2 X 3 = 6 2 X 4 = 8 2 X 5 = 10 2 X 6 = 12
3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 3 X 4 = 12 3 X 5 = 15 3 X 6 = 18
4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 4 X 5 = 20 4 X 6 = 24
5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 5 X 6 = 30
6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36
我们可以看到,break语句在此只跳出了内循环,当然这也确实是break语句的功能,细心一点你可以看到上面的样例中输出了三行换行符。
然后我们尝试标签跳转,直接跳到外循环:
func main() {
var i, j int
label1:
for i = 1; i < 10; i++ {
for j = 1; j < 10; j++ {
if i>6 || j>6 {
break label1;
}
fmt.Printf("%d X %d = %d ", i, j, i*j)
}
fmt.Print("\n")
}
}
#输出
1 X 1 = 1 1 X 2 = 2 1 X 3 = 3 1 X 4 = 4 1 X 5 = 5 1 X 6 = 6
-
continue
篇幅关系,我们直接上标签样例,跟不加标签的区别就在于没有打出三行换行:
func main() {
var i, j int
label2:
for i = 1; i < 10; i++ {
for j = 1; j < 10; j++ {
if i<=3 {
continue label2;
}
fmt.Printf("%d X %d = %d ", i, j, i*j)
}
fmt.Print("\n")
}
}
#输出
4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 4 X 5 = 20 4 X 6 = 24 4 X 7 = 28 4 X 8 = 32 4 X 9 = 36
5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 5 X 6 = 30 5 X 7 = 35 5 X 8 = 40 5 X 9 = 45
6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 6 X 7 = 42 6 X 8 = 48 6 X 9 = 54
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 7 X 8 = 56 7 X 9 = 63
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 8 X 9 = 72
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
-
goto
借助goto配合标签使用,我们来重新优化下我们的样例,顺便体会下goto的标签用法:
func main() {
smail99()
}
func smail99() {
for i := 1; i < 10; i++ {
j:=1
label3:
if j<=i {
fmt.Printf("%d X %d = %d ", i, j, i*j)
j++
goto label3;
}
fmt.Print("\n")
}
}
#输出
1 X 1 = 1
2 X 1 = 2 2 X 2 = 4
3 X 1 = 3 3 X 2 = 6 3 X 3 = 9
4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16
5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25
6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36
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
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
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
网友评论