当前位置:网站首页>go 分支与循环
go 分支与循环
2022-07-02 03:45:00 【UPythonFish】
go 分支与循环
if-else 语句
go 的 if-else 判断语句,和js很像,语法如下
if condition {
} else if condition {
} else {
}
当 condition 满足条件就执行 { } 中的代码,不满足就执行 else 后面
func main() {
number := 55
if number >=90 && number <= 100 {
// 大括号的开始需要和关键字在一行
// go中默认没一行结尾都会带上 ; 换行就会报错
fmt.Println("优秀")
} else if number >= 60 && number <= 90 {
// else if 后面必须带条件
fmt.Println("良好")
} else {
// else 后面不能带条件
fmt.Println("不及格")
}
}
for 循环语句
for
是 Go 语言唯一的循环语句。Go 语言中并没有其他语言比如 python 中的 while 循环。
完整的 for 循环 语法
for initialisation; condition; post {
}
初始化语句只执行一次。循环初始化后,将检查循环条件。如果条件的计算结果为 true
,则 {}
内的循环体将执行,接着执行 post 语句。post 语句将在每次成功循环迭代后执行。在执行 post 语句后,条件将被再次检查。如果为 true
, 则循环将继续执行,否则 for 循环将终止。(译注:这是典型的 for 循环三个表达式,第一个为初始化表达式或赋值语句;第二个为循环条件判定表达式;第三个为循环变量修正表达式,即此处的 post )
for 循环的 5 种使用方式
1. 基于索引的循环
func main() {
// 循环打印 0-9
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}
2. 外部初始化
func main(){
i := 0 // i 的作用域扩大了
for ; i<10; i++{
// ; 不能省略
fmt.Println(i)
}
}
3. 循环变量修正表达式写在内部
func main(){
i := 0 // i 的作用域扩大了
for ; i<10; {
// 只写条件 可以省略 ;
fmt.Println(i)
i++
}
}
func main(){
i := 0 // i 的作用域扩大了
for i<10 {
// 只写条件 可以省略 ;
fmt.Println(i)
i++
}
}
4. 省略循环条件
func main(){
for ; ; {
// 此时 ; 也可以省略
fmt.Println("死循环")
}
}
func main(){
for {
// 此时 ; 也可以省略
fmt.Println("死循环")
break // 可以加上 break 关键字来终止,同样也有 continue 关键字,直接跳过下面代码,执行下一次循环
}
}
5. 基于迭代的循环
func main(){
var a = [3]int[1, 25, 34] // 数组
for i,value := range a {
fmt.Println(i) // 索引
fmt.Println(value) // 值
}
}
go 种 for 循环同 其他语言种一样,也可以执行 循环嵌套,下面列举打印出9 x 9 乘法表
func main() {
for i := 1; i < 10; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d x %d = %d ", j, i, i*j)
}
fmt.Println()
}
}
switch 语句
switch 是一个条件语句,用于将表达式的值与可能匹配的选项列表进行比较,并根据匹配情况执行相应的代码块。它可以被认为是替代多个 if else
子句的常用方式。
// 1 使用方式一
name:="tom"
switch name {
case "lxx":
fmt.Println("lxx")
case "tom":
fmt.Println("tom")
}
// 使用方式2 不带条件
name:="tom"
switch {
case name=="lxx":
fmt.Println("lxx")
case name=="tom":
fmt.Println("tom")
}
// 使用方式3 多条件
name:="tom"
switch name {
case "lxx","jason": // 或者
fmt.Println("lxx")
case "tom","marry":
fmt.Println("tom")
}
name := "tom"
switch {
case name == "lxx" || name == "jason": // 或者
fmt.Println("lxx")
case name == "tom", name == "marry":
fmt.Println("tom")
}
// 4 方式4 ,default的使用,不符合所有case后,执行
name:="tom"
switch name {
case "lxx":
fmt.Println("lxx")
case "tom":
fmt.Println("tom")
default:
fmt.Println("不知道是谁")
}
// 5 fallthrough的使用 只要看到这个关键字,就无条件执行下一个case
name:="lxx"
switch name {
case "lxx":
fmt.Println("lxx")
fallthrough
case "tom":
fmt.Println("tome")
fallthrough
default:
fmt.Println("不知道是谁")
}
注意事项:
- case 后面不允许出现重复项选项,若重复会报错
边栏推荐
- SQL:常用的 SQL 命令
- 树莓派GPIO引脚控制红绿灯与轰鸣器
- Class design basis and advanced
- 一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
- 【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
- The first practical project of software tester: web side (video tutorial + document + use case library)
- 蓝桥杯单片机省赛第六届
- NLog use
- Eight steps of agile development process
- 蓝桥杯单片机省赛第十一届第二场
猜你喜欢
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
Review materials of project management PMP high frequency examination sites (8-1)
Account management of MySQL
蓝桥杯单片机省赛第九届
蓝桥杯单片机第四届省赛
u本位合约爆仓清算解决方案建议
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
The fourth provincial competition of Bluebridge cup single chip microcomputer
树莓派GPIO引脚控制红绿灯与轰鸣器
Exchange rate query interface
随机推荐
毕设-基于SSM电影院购票系统
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
Network connection mode of QT
The 10th Blue Bridge Cup single chip microcomputer provincial competition
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
[designmode] Prototype Pattern
蓝桥杯单片机省赛第五届
Kotlin basic learning 13
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
Oracle viewing locked tables and unlocking
Jetpack之LiveData扩展MediatorLiveData
傅里叶级数
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
Failed to upgrade schema, error: “file does not exist
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
Fourier series