当前位置:网站首页>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 后面不允许出现重复项选项,若重复会报错
边栏推荐
- Oracle的md5
- Kotlin basic learning 17
- Gradle foundation | customize the plug-in and upload it to jitpack
- 蓝桥杯单片机数码管技巧
- The 8th Blue Bridge Cup single chip microcomputer provincial competition
- The 9th Blue Bridge Cup single chip microcomputer provincial competition
- [punch in] flip the string (simple)
- Blue Bridge Cup single chip microcomputer sixth temperature recorder
- A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
- Pycharm2021 delete the package warehouse list you added
猜你喜欢

The 8th Blue Bridge Cup single chip microcomputer provincial competition
![[mv-3d] - multi view 3D target detection network](/img/aa/741b36ead2dfaa5a165401b8d657b7.jpg)
[mv-3d] - multi view 3D target detection network

Gradle foundation | customize the plug-in and upload it to jitpack

微信小程序中 在xwml 中使用外部引入的 js进行判断计算

The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

【小技巧】使用matlab GUI以对话框模式读取文件

The 10th Blue Bridge Cup single chip microcomputer provincial competition

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

Kubernetes cluster storageclass persistent storage resource core concept and use

Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
随机推荐
Fourier series
Kotlin basic learning 14
向数据库中存入数组数据,代码出错怎么解决
In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
Knowing things by learning | self supervised learning helps improve the effect of content risk control
Interface debugging tool simulates post upload file - apipost
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
Gradle foundation | customize the plug-in and upload it to jitpack
Jetpack之LiveData扩展MediatorLiveData
Kubernetes cluster storageclass persistent storage resource core concept and use
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
[mv-3d] - multi view 3D target detection network
微信小程序中 在xwml 中使用外部引入的 js进行判断计算
【小技巧】使用matlab GUI以对话框模式读取文件
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
The first practical project of software tester: web side (video tutorial + document + use case library)
The page in H5 shows hidden execution events
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
Failed to upgrade schema, error: “file does not exist
The fourth provincial competition of Bluebridge cup single chip microcomputer