程序的流程控制结构共有三种:顺序结构、选择结构、循环结构
- 顺序结构:从上到下,逐步执行,默认的逻辑
- 选择结构:条件满足某些代码才会执行
- if,else if, else
- switch
- select ,channel
- 循环结构,条件满足某些代码被反复执行0-N次
- for
1、if语句
package main
import "fmt"
func main() {
var score int = 50
if score >= 90 && score <= 100 {
fmt.Println("A")
} else if score >= 80 && score < 90 {
fmt.Println("B")
} else if score >= 70 && score < 80 {
fmt.Println("C")
} else if score >= 60 && score < 70 {
fmt.Println("D")
} else {
fmt.Println("不及格")
}
}
if 多重嵌套
func main() {
var a, b int
var pwd int = 20221020
// 验证密码正确
fmt.Print("请输入密码:")
fmt.Scan(&a) // 阻塞等待输入,输入值赋值到变量a
// 业务:验证密码输入是否正确
if a == pwd {
fmt.Print("请再次输入密码:")
fmt.Scan(&b)
if b == pwd {
fmt.Println("登录成功")
} else {
fmt.Println("登录失败,第二次输入密码错误2022")
}
} else {
fmt.Print("登录失败,密码输入错误")
}
}
执行结果:
请输入密码:20221020
请再次输入密码:20221020
登录成功
2、switch语句
switch
go语言同样提供switch语句,与java一样,switch 适合枚举值的判断,语法:
switch var0 {
case var1 :
...
case var2 :
...
default :
...
}
示例
package main
import "fmt"
func main() {
var score int = 90
// 匹配
switch score {
case 90,93,92: // 多个匹配值
fmt.Println("A")
case 80:
fmt.Println("B")
case 60,70:
fmt.Println("C")
default:
fmt.Println("D")
}
}
// switch 默认的条件 bool = true
switch {
case false:
fmt.Println("false")
case true:
fmt.Println("true")
default:
fmt.Println("其他")
}
执行结果:
true
fallthrough 贯穿
switch默认情况下匹配成功后就不会执行其他case,如果我们需要执行后面的case,可以使用fallthrough
穿透case
使用fallthrough 会强制执行后面的case语句,不会去判断下一条case的表达式结果是否为true
func main() {
a := false
switch a {
case false:
fmt.Println("1、case条件为false")
case true:
fmt.Println("2、case条件为true")
}
}
执行结果:
1、case条件为false
增加fallthrough后
func main() {
a := false
switch a {
case false:
fmt.Println("1、case条件为false")
fallthrough // case穿透,不管下一个条件是否满足,都会执行
case true:
fmt.Println("2、case条件为true")
default:
fmt.Println("3、其他")
}
}
执行结果:
1、case条件为false
2、case条件为true
增加break跳出switch,结束fallthrough
func main() {
a := false
switch a {
case false:
fmt.Println("1、case条件为false")
fallthrough // case穿透,不管下一个条件是否满足,都会执行
case true:
if a == false {
break // 跳出switch
}
fmt.Println("2、case条件为true")
default:
fmt.Println("3、其他")
}
}
执行结果:
1、case条件为false
多个fallthrough
func main() {
var score int = 90
// 匹配
switch score {
case 90:
fmt.Println("A")
fallthrough
case 80:
fmt.Println("B")
fallthrough
case 60, 70:
fmt.Println("C")
fallthrough
default:
fmt.Println("D")
}
}
执行结果:
A
B
C
D
3、for循环
go只有for循环,循环的本质是做一些重复性操作
格式1:for 条件的起始值;循环条件;控制变量自增或自减
func main() {
sum := 0
// golang 的快捷键 fori
for i := 1; i <= 10; i++ {
sum = sum + i
}
fmt.Printf("总和:%d", sum)
}
格式2: for ;循环条件;
func main() {
sum := 0
i := 1
for ; i <= 10; {
sum = sum + i
i++
}
fmt.Printf("总和:%d", sum)
}
格式3:for {} 无限循环
func main() {
i := 1
for {
fmt.println(i)
i++
}
}
无限循环,不建议使用
for {
fmt.Println("hello world")
time.Sleep(1*time.Second) // 线程睡眠1小时
// 比如睡眠1小时10分5秒:
time.Sleep(1*time.Hour + 10*time.Minute + 5*time.Second)
}
打印方阵
for j := 0; j < 5; j++ {
for i := 0; i < 5; i++ {
fmt.Print("* ")
}
fmt.Println()
}
执行结果:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
打印99乘法表
func main() {
for i := 1; i <= 9; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d*%d=*%d |", j, i, j*i)
}
fmt.Println()
}
}
执行结果:
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 |
打印菱形
func main() {
for i := 1; i <=6 ; i++ {
// 递减循环打印空格 5 4 3 2 1
for j := 1; j <=6-i ; j++ {
fmt.Print(" ")
}
// 递增实现打印*号 1 3 5 与外层的关系是2i-1
for j := 1; j <= 2*i-1 ; j++ {
fmt.Print("*")
}
fmt.Println(" ")
}
for i := 5; i >=1 ; i-- {
// 递增循环打印空格 1 2 3 4 5
for j := 1; j <=6-i ; j++ {
fmt.Print(" ")
}
// 递减循环打印*号 5 3 1 与外层的关系是2i-i
for j := 1; j <=2*i-1 ; j++ {
fmt.Print("*")
}
fmt.Println(" ")
}
}
执行结果
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
break与continue
break结束当前整个循环
for i := 0; i < 10; i++ {
if i == 5 {
break
}
fmt.Println(i)
}
continue 结束当次循环
for i := 0; i < 10; i++ {
if i == 5 {
continue
}
fmt.Println(i)
}
遍历string
len()函数
func main() {
str := "hello,xuexiangban"
fmt.Println(str)
// 获取字符串的长度
//fmt.Printf("字符串的长度:%d", len(str))
fmt.Println("字符串的长度:", len(str))
// 获取指定字节,会发现打印的是ASCII编码表中字母h的整型值104,十进制值
fmt.Println("字节打印:", str[0]) // 字符串的下标index从0开始
// 打印
fmt.Printf("%c", str[0])
fmt.Printf("%c", str[1])
}
执行结果:
hello,xuexiangban
字符串的长度: 17
字节打印: 104
he
for range循环,遍历字符串,数组,切片
func main() {
str := "hello,xuexiangban"
// for range循环,遍历数组,切片
// i是下标,v是值
for i, v := range str {
fmt.Printf("下标:%d,值:%c\n", i, v) // 格式化输出
}
}
执行结果:
下标:0,值:h
下标:1,值:e
下标:2,值:l
下标:3,值:l
下标:4,值:o
下标:5,值:,
下标:6,值:x
下标:7,值:u
下标:8,值:e
....