当前位置:网站首页>switch分支
switch分支
2022-04-21 19:15:00 【wx6235761444879】
简单来看看switch分支的结构
switch(
整形表达式)
//在英语中switch 是判断的意思
{
语句项;
}
- 1.
- 2.
- 3.
- 4.
有没有发现这里不是语句,而是语句项。那么语句项是什么意思呢?
语句项就是case语句,下面我们来写一段代码感受感受
int
main()
{
int
day
=
0;
scanf(
"%d\n",
&
day);
switch(
day)
{
case
1:
//情况1
printf(
"星期一\n");
break;
case
2:
//情况2
printf(
"星期二\n");
break;
case
3:
//情况3
printf(
"星期三\n");
break;
case
4:
//情况4
printf(
"星期四\n");
break;
case
5:
//情况5
printf(
"星期五\n");
break;
case
6:
//情况6
printf(
"星期六\n");
break;
case
7:
//情况7
printf(
"星期七\n");
break;
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
怎么来理解呢?这个case到底是个啥,起初我也不知道,这个例子就可以很好的说明case是干啥的,好,来,走

看到这每个情况,case里面的代码块都是一样的内容,可以省吗,又怎么去省呢?
来,走
//switch分支省略
int
main()
{
int
day
=
0;
scanf(
"%d\n",
&
day);
switch(
day)
{
case
1:
case
2:
case
3:
case
4:
case
5:
printf(
"工作日\n");
break;
case
6:
case
7:
printf(
"休息日\n");
break;
default:
//default是默认的意思
printf(
"输入错误\n");
break;
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
我们已经对switch 分支有一定的了解,那么下面给个例子来深层理解吧!走!
int
main()
{
int
n
=
1;
int
m
=
2;
switch(
n)
{
case
1:
m
++;
case
2:
n
++;
case
3:
switch(
n)
//switch分支可以嵌套
{
case
1:
n
++;
case
2:
m
++,
n
++;
}
case
4:
m
++;
default:
break;
}
printf(
"m=%d,n=%d\n",
m,
n);
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
自己先想一想思路,想想是否符合逻辑,再看下面的思路

总结:switch分支中case和break,default
case的理解至关重要,switch分支整型表达式中的值就应case n;然后从case n 这里开始向下执行,并不是一一对应关系,并不是输入多少就答应case n这种情况。
另外注意switch 分支是可以嵌套的
感谢观看!慢慢改进,让我的博客易懂也更有意思
版权声明
本文为[wx6235761444879]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_15555633/5239246
边栏推荐
- [leetcode] daily question: goat Latin
- Use the mremoting tool to manage all remote connections
- EdgeBoard记录
- 国标GB28181平台EasyGBS如何开启语音对讲功能?
- EasyGBS关闭了录像计划,为何还有录像文件生成?
- robotframework日志输出中文乱码
- 到底什么是外包?
- Mysql database index interview questions (latest version)
- Wide application of medical robot in AI field
- Edgeboard records
猜你喜欢

How to open the developer tool when the uniapp runs to the built-in browser

Sorting will be recursive. It's a pity not to learn non recursive

Using MCU Xpress to develop rt1060 (1) -- drive RGB interface LCD

Automatic control principle Chapter 5 - frequency method (mind map)

Can the stadium system be connected to other intelligent devices

How to classify cosmetics in the management system?

Analysis of service registration source code of Nacos

Sleuth + Zipkin link tracking

Using 8266 as serial port debugging tool 2

Rk3399 - add USB to serial driver
随机推荐
机器学习笔记 - 迹算子(跟踪运算符)
MusicPlayer2. Version 1
APM industry awareness series - VII - 17 Ways to define Devops
SIGIR'22 "Ali" metacvr: meta learning alleviates the problem of data distribution fluctuation in small-scale recommendation
In the El input search in ement, the matching input suggestions after input must have the value attribute
力扣-53.最大子数组和
Pat (advanced level) 1096 - continuous
化妆品在管理系统中如何分类?
MKL library matrix multiplication
How to open the developer tool when the uniapp runs to the built-in browser
Easygbs has closed the video recording plan. Why are video files still generated?
MySQL数据库索引面试题(最新版)
Rk3399—添加usb转串口驱动
Some keywords of the robotframework cannot be used or are black
MySQL throughput
2022.04.21 (lc_452_detonates the balloon with the least number of arrows)
EasyGBS关闭了录像计划,为何还有录像文件生成?
Context in programming
JVM topic (XI) - garbage collection (II)
XD signal and system