当前位置:网站首页>(六)控制语句if/else switch
(六)控制语句if/else switch
2022-06-12 16:43:00 【二进制杯莫停】
1 if/else
1.1 常规写法
if 语句 后可以使用可选的 else 语句, else 语句中的表达式在布尔表达式为 false 时执行。
if 布尔表达式 {
/* 在布尔表达式为 true 时执行 */
} else {
/* 在布尔表达式为 false 时执行 */
}
实例
package main
import "fmt"
func main() {
/* 局部变量定义 */
var a int = 100;
/* 判断布尔表达式 */
if a < 20 {
/* 如果条件为 true 则执行以下语句 */
fmt.Printf("a 小于 20\n" );
} else {
/* 如果条件为 false 则执行以下语句 */
fmt.Printf("a 不小于 20\n" );
}
fmt.Printf("a 的值为 : %d\n", a);
}
1.2 特殊写法
if 初始化语句; 条件 {
}
实例:
package main
import "fmt"
func main() {
if num := 10; num %2 == 0 {
fmt.Println(num, "is even")
} else {
fmt.Println(num, "is odd")
}
}
2 switch
2.1 标准写法
switch 语句用于基于不同条件执行不同动作,每一个 case 分支都是唯一的,从上至下逐一测试,直到匹配为止。
switch 语句执行的过程从上至下,直到找到匹配项,匹配项后面也不需要再加 break。
switch 默认情况下 case 最后自带 break 语句,匹配成功后就不会执行其他 case,如果我们需要执行后面的 case,可以使用 fallthrough 。
switch var1 {
case val1:
...
case val2:
...
default:
...
}
实例:
package main
import "fmt"
func main() {
/* 定义局部变量 */
var grade string = "B"
var marks int = 90
switch marks {
case 90: grade = "A"
case 80: grade = "B"
case 50,60,70 : grade = "C"
default: grade = "D"
}
}
2.2 省略变量的写法
省略switch后面的变量,相当于直接作用在true上
package main
import "fmt"
func main() {
switch {
case true:
fmt.Printf("yes!\n" )
case false :
fmt.Printf("no!\n" )
default:
fmt.Printf("....\n" );
}
}
边栏推荐
- Preprocessing command section 3
- Recommend AI intelligent drawing repair software
- Idea displays services on the console to uniformly manage all jetty services,
- Uniapp壁纸小程序源码/双端微信抖音小程序源码
- Leetcode 2194. Cells within a range in Excel table (yes, solved)
- [research] reading English papers -- the welfare of researchers in English poor
- [DSP video tutorial] DSP video tutorial Issue 8: performance comparison of DSP library trigonometric function, C library trigonometric function and hardware trigonometric function, and accuracy compar
- 1.delete
- mysql语句
- 云开发坤坤鸡乐盒微信小程序源码
猜你喜欢

The C programming language (version 2) notes / 8 UNIX system interface / 8.7 instance (storage allocator)

Swin Transformer代码讲解

如何基于CCS_V11新建TMS320F28035的工程

Cookies and sessions

丁总路由器设置以及401联网

软件工程 学生信息管理系统 结构化的需求分析

薛定谔的日语学习小程序源码

Joint recruitment notice of ganfei research group of Wuhan University and xuzhenjiang research group of Nanchang University

About component value transfer
![[MySQL] Cartesian product - multi table query (detailed explanation)](/img/46/6a9a62b35eaa538232da1d738b3931.jpg)
[MySQL] Cartesian product - multi table query (detailed explanation)
随机推荐
Recommend 6 open source projects of yyds
Probation period and overtime compensation -- knowledge before and after entering the factory labor law
Double write consistency problem
Why is your next computer a computer? Explore different remote operations
【湖南大学】考研初试复试资料分享
calibration of sth
RMI, JNDI, LDAP introduction +log4j vulnerability analysis
Which colleges are particularly easy to enter?
mysql语句
pbootcms的if判断失效直接显示标签怎么回事?
js 使用Rsa 加密 解密
Extract the new Chinese cross modal benchmark zero from 5billion pictures and texts, and Qihoo 360's new pre training framework surpasses many SOTAS
Project training of Shandong University rendering engine system (VI)
如何基于CCS_V11新建TMS320F28035的工程
Demande de doctorat | xinchao Wang, Université nationale de Singapour
QCustomplot笔记(一)之QCustomplot添加数据以及曲线
MySQL statement
Cookies and sessions
What's the matter with pbootcms' if judgment failure and direct display of labels?
Collect | 22 short videos to learn Adobe Illustrator paper graphic editing and typesetting