当前位置:网站首页>深入C语言(4)——switch的定义与使用
深入C语言(4)——switch的定义与使用
2022-07-29 03:25:00 【据说这是zzy】
深入C语言(4)——switch的定义与使用
全文中使用的C标准为C99,ANSI C,如果没有特别说明默认为此。
C++则是Dev 5.7.1的C++版本。
C的Switch和case
常见语法:
switch (expression)
statement
其中expression的结果必须是整数值。
尽管直接使用一条语句也是可以的,但结果效果如下:
switch (expression)
{
statement-list
}
贯穿于statement-list 的标签如下:
case constant-expression:
每一个case标签必须具有一个唯一的值:
常量表达式(constant-expression),constant-expression指的是编译期间进行求值的表达式,他不能是任何变量。
case与执行流
case语句最特殊的点在于,它并不把语句划分成几个执行部分,而是通过case来确定语句列表的进入点。
执行过程如下:
- 计算完成expression的内容。
- 执行流转到语句列表中其case标签值与expression值匹配的语句。
- 从上一条语句执行开始,一直到switch-case的底部。
这里面的语句均会被执行。
举个例子:
switch (month)
{
case 12:
printf("%d",12);
case 11:
printf("%d",11);
}
结果呢,可能和你想象的不一样。
当month等于12的时候,结果如下:
1211
这就是执行流造成的结果,导致case标签后边的语句都会被执行,包括其他的case。
那么可以实现将代码划分为不同的部分的功能吗?当然有,break语句就是。
break语句
break语句使用很简单,就是让case流在不同的位置停下来。让switch以更传统的方式运行。
使用方法如下:
switch (month)
{
case 12:
printf("%d",12);
break;
case 11:
printf("%d",11);
case 10:
printf("%d",10);
}
结果如下:
12
12
--------------------------------
Process exited after 4.971 seconds with return value 0
请按任意键继续. . .
break的习惯
需要提醒的是,最后的case的需要break吗?理论上来说传统的break语法中,我们不需要在最后的case中添加break。因为没有case会继续执行,但是为了未来修改的一致性,我们还是在最后加上break,以后添加case选项的时候就不需要补充了。
为了让不同的表达式值时能够执行,可以使多个case标签对应,如下所示:
switch (month)
{
case 12:
printf("%d",12);
break;
case 11:
case 10:
printf("%d %d",10,11);
break;
}
结果如下:
11
1011
--------------------------------
10
1011
--------------------------------
default子句
如果存在一种情况,所有case的标签值都不匹配,但是你想找一个兜底的条件,就可以使用default子句,把下面的标签放到case可以出现的位置。
default:
也就是说你可以这样写:
switch (month)
{
case 12:
printf("%d",12);
break;
default :
printf("%s","default");
case 11:
case 10:
printf("%d %d",10,11);
break;
}
结果如下:
9
default1011
--------------------------------
这样就丧失了default的意义,不过还是很有趣hhhh。
边栏推荐
- Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding
- 照片比例校正工具:DxO ViewPoint 3 直装版
- How to deploy sentinel cluster of redis
- Sleuth+Zipkin 来进行分布式服务链路的追踪
- Producer consumer model of concurrent model
- [freeswitch development practice] unimrcp compilation and installation
- Web uploader cannot upload multiple files
- Simple code implementation of K-means clustering
- 数字图像处理 第10章——图像分割
- RTP 发送 和接收 h265
猜你喜欢

Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names

基于单片机烟雾温湿度甲醛监测设计

ShardingSphere之水平分表实战(三)

Rongyun IM & RTC capabilities on new sites

Sleuth+Zipkin 来进行分布式服务链路的追踪

3D advanced renderer: artlandis studio 2021.2 Chinese version

ROS - create workspace

Unity 之游戏特效

Makefile details

mysql的timestamp存在的时区问题怎么解决
随机推荐
Introduction to JVM foundation I (memory structure)
STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
数字图像处理 第10章——图像分割
3D advanced renderer: artlandis studio 2021.2 Chinese version
A simple and general method to obtain the size of function stack space
Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names
How close can QA be to business code Direct exposure of defects through codediff
美联储再加息,75基点 鲍威尔“放鸽”,美股狂欢
A case of gradually analyzing the splitting of classes -- colorful ball collisions
GJB常见混淆概念
Three military product baselines (functional baseline, distribution baseline, product baseline) and the documents contained in the baseline
Rongyun real-time community solution
Kubernetes-1.24.x feature
[robot learning] matlab kinematics and ADMAS dynamics analysis of manipulator gripper
Learn exkmp again (exkmp template)
军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件
three.js 第五十四用如何给shader传递结构体数组
Sleuth+Zipkin 来进行分布式服务链路的追踪
Suffix automata (SAM) board from Jly
力扣刷题之分数加减运算(每日一题7/27)