当前位置:网站首页>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 后面不允许出现重复项选项,若重复会报错
边栏推荐
- Qt的网络连接方式
- Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
- The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
- Review materials of project management PMP high frequency examination sites (8-1)
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
- [yolo3d]: real time detection of end-to-end 3D point cloud input
- Account management of MySQL
- It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
- Blue Bridge Cup SCM digital tube skills
- 【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
猜你喜欢
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
MySQL之账号管理
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
蓝桥杯单片机省赛第六届
《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算
Visual slam Lecture 3 -- Lie groups and Lie Algebras
In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
The 6th Blue Bridge Cup single chip microcomputer provincial competition
[designmode] Prototype Pattern
随机推荐
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
Kotlin基础学习 15
MySQL之账号管理
Introduction to Robotics II. Forward kinematics, MDH method
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算
VS2010 plug-in nuget
Influence of air resistance on the trajectory of table tennis
u本位合约爆仓清算解决方案建议
集成底座方案演示说明
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
NLog use
Kotlin基础学习 16
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
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
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
Network connection mode of QT
蓝桥杯单片机省赛第九届