当前位置:网站首页>C language -- 12 branch statement switch
C language -- 12 branch statement switch
2022-06-10 18:15:00 【Try!】
1、 understand switch Branch statement
switch Statement is also a branch statement , It is often used in the case of multiple branches .
such as :
Input 1, It will output Monday
Input 2, Will output Tuesday
…
Input 7, It will output on Sunday
For such code requirements , If you use if 、else if、else The form of will appear too complex . How to use switch Statement to realize this function ?switch What is the structure of the statement ?
switch( Integer expression )
{
Statement item ;
}
among , An integer expression must be an integer , And is a constant ( Characters are OK , Because characters are also a kind of integer , Because characters are stored in characters ASCII Code value ); Statement items are some case sentence .
case Integral constant expression ;
sentence ;
Code implementation :
int main()
{
int day = 0;
scanf("%d",&day);
switch (day)
{
case 1:
printf(" Monday \n");
case 2:
printf(" Tuesday \n");
case 3:
printf(" Wednesday \n");
case 4:
printf(" Thursday \n");
case 5:
printf(" Friday \n");
case 6:
printf(" Saturday \n");
case 7:
printf(" Sunday \n");
}
return 0;
}
Run the code , The results obtained after the discovery run are as follows :
1
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
obviously , Such a result is wrong . Because after the execution case 1 after , Nothing can stop it , It will always be case 2、case 3… Continue to carry out . If the program is running , The input is 6, Will be output Saturday, Sunday .switch The following expression (day) How many , It will come from case A few in , What determines the entry is the expression day And case Value after .
Modify the program : At every case Followed by break, To jump out case.case The decision is the entrance , and break The decision is to export , This is the real branch .
2、 Example : Input 1-5 Just output the working days , Input 6-7 Just output the rest day .
int main()
{
int day = 0;
scanf("%d",&day);
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf(" Working day \n");
break;
case 6:
case 7:
printf(" Rest Day \n");
break;
}
return 0;
}
The results of the test run are as follows :
5
Working day
break The actual effect of the statement is to divide the statement list into different parts . Have a good habit of programming : In the last case Add a clause after the statement break sentence . In case we need to add something else on the basis of the original procedure case sentence , The above input will not appear 1, Print out all the information from Monday to Sunday .
3、default Clause
If the expressed value is the same as all case What if the values of the tags don't match ? It's nothing , The result is that all statements are skipped , The program does not terminate , No errors reported . Because this situation is C It is not considered a mistake in language , however , What if you don't want to ignore expressions that don't match all the tags ? At this time, you can add a sentence to the statement list default Clause , Write it down in any one case Where labels can appear .
When switch The value of the expression does not match all case The value of the tag , This default The statement following the clause will execute . Be careful : Every switch Only one... Can appear in the statement default Clause .
int main()
{
int day = 0;
scanf("%d",&day);
switch (day)
{
default:
printf(" Input error \n");
break;
case 1:
printf(" Monday \n");
break;
case 2:
printf(" Tuesday \n");
break;
case 3:
printf(" Wednesday \n");
break;
case 4:
printf(" Thursday \n");
break;
case 5:
printf(" Friday \n");
break;
case 6:
printf(" Saturday \n");
break;
case 7:
printf(" Sunday \n");
break;
}
return 0;
}
test result :
9
Input error
4、 practice
Look at the result of the program ?
int main()
{
int n = 1;
int m = 2;
switch (n)
{
case 1 :
m++;
case 2:
n++;
case 3:
switch (n)
//switch You can nest
{
case 1:
n++;
case 2:
m++;
n++;
break;
}
case 4:
m++;
break;
default:
break;
}
printf("m=%d,n=%d\n",m,n);
return 0;
}
The main thing is to investigate switch Inside break The role of .
The final running results are as follows :
m=5,n=3
边栏推荐
- 领导提拔你的原因,只有这点最真实,其他都是瞎扯!
- Wireshark学习笔记(一)常用功能案例和技巧
- 微信小程序仿陶票票课程设计
- 电商行业转账返款方案分析
- The latest good article | interpretable confrontation defense based on causal inference
- [FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
- XML & XPath parsing
- c语言学习回顾---1 基础知识回顾
- Swin_Transformer源码解读
- 【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇
猜你喜欢
随机推荐
pwnable start
IP总结(TCP/IP卷1和卷2)
CUDA realizes efficient search - failed audit?
CodeCraft-22 and Codeforces Round #795 (Div. 2)
关于目前CIM(BIM+GIS)行业的一些看法
Leetcode 875. 爱吃香蕉的珂珂
领导提拔你的原因,只有这点最真实,其他都是瞎扯!
待办事项桌面插件,办公族的桌面好帮手
Analysis of transfer Refund Scheme in e-commerce industry
Wireshark learning notes (I) common function cases and skills
Play with pytoch's function class
THE LOTTERY TICKET HYPOTHESIS: FINDING SPARSE, TRAINABLE NEURAL NETWORKS论文笔记
最新好文 | 基于因果推断的可解释对抗防御
一个WPF开发的打印对话框-PrintDialogX
mmcv之Registry类解读
c语言---12 分支语句switch
优惠券的工厂与策略模式实现方案
Protocol Gen go grpc 'is not an internal or external command, nor is it a runnable program or batch file
Vim常用命令总结
Abbkine柱式法ExKine Pro动物细胞/组织总蛋白提取试剂盒









