当前位置:网站首页>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 后面不允许出现重复项选项,若重复会报错
边栏推荐
- The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
- The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
- Unity脚本的基础语法(7)-成员变量和实例化
- Kubernetes cluster storageclass persistent storage resource core concept and use
- Kotlin basic learning 14
- The 10th Blue Bridge Cup single chip microcomputer provincial competition
- How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
- "Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
- MySQL index, transaction and storage engine
- 整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
猜你喜欢

The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition

Didi open source Delta: AI developers can easily train natural language models

Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface

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

蓝桥杯单片机省赛第十届

Kubernetes cluster storageclass persistent storage resource core concept and use

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

Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)

《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真

C语言:逻辑运算和判断选择结构例题
随机推荐
How about Ping An lifetime cancer insurance?
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
Blue Bridge Cup SCM digital tube skills
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
蓝桥杯单片机省赛第十一届
集成底座方案演示说明
High performance and low power cortex-a53 core board | i.mx8m Mini
蓝桥杯单片机第四届省赛
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
Account management of MySQL
MySQL index, transaction and storage engine
初识string+简单用法(二)
Qt的网络连接方式
Object oriented thinking
Kotlin基础学习 14
【DesignMode】原型模式(prototype pattern)
Oracle 查看被锁的表和解锁
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
JS generate random numbers