当前位置:网站首页>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 后面不允许出现重复项选项,若重复会报错
边栏推荐
- Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
- [ibdfe] matlab simulation of frequency domain equalization based on ibdfe
- Exchange rate query interface
- 蓝桥杯单片机省赛第八届
- Interface debugging tool simulates post upload file - apipost
- 《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算
- Unity脚本的基础语法(6)-特定文件夹
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- 5g era is coming in an all-round way, talking about the past and present life of mobile communication
- Gradle foundation | customize the plug-in and upload it to jitpack
猜你喜欢

In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews

蓝桥杯单片机第四届省赛
![[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification](/img/77/4df7a1439ff1a53f94d409a19a47d6.png)
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification

潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)

0基础如何学习自动化测试?按照这7步一步一步来学习就成功了

【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!

The 10th Blue Bridge Cup single chip microcomputer provincial competition

【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真

蓝桥杯单片机省赛第十二届第二场

Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
随机推荐
First acquaintance with string+ simple usage (II)
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
NLog use
Exchange rate query interface
[mv-3d] - multi view 3D target detection network
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
SQL:常用的 SQL 命令
The 9th Blue Bridge Cup single chip microcomputer provincial competition
【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
How about Ping An lifetime cancer insurance?
蓝桥杯单片机省赛第五届
Didi open source Delta: AI developers can easily train natural language models
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
Oracle 查看被锁的表和解锁
Oracle viewing locked tables and unlocking
Blue Bridge Cup single chip microcomputer sixth temperature recorder
Knowing things by learning | self supervised learning helps improve the effect of content risk control