当前位置:网站首页>On the usage details and special usage of switch case
On the usage details and special usage of switch case
2022-06-12 23:57:00 【Guigui-23】
About switch-case Of Basic usage Everyone must have known something about it , I won't go over it here . Let's go straight to the point , Talk about using switch-case Some of Little details .
Detail one
switch ( expression )
{
case result 1:
sentence 1;
break;
case result 2:
sentence 2;
break;
....
default:
sentence n;
break;
}For example, the above form , among switch after expression The value of must be An integer value ( for example : integer , Character , Enumeration, etc ), And the value must be the same as case The result type is the same ;case Can only be Constant or constant expression ;
...
int a=2,b=2;
switch (a)
{
case b:
printf("o");
default :
printf("s");
}
...The execution result is : Compile error . because case After b It's a variable. , Even though he was assigned .
Detail two
...
switch (2)
{
default :
printf("s");
case 1:
printf("o");
}
...In the above code , The running result is :so
Because assembly code interpretation switch The way of statement is : Judge first if there is case Matches , If not, you will arrive default To execute .
default It can be placed anywhere , But the execution order is always at the end of the branch .
Detail three
Please compare the three programs and distinguish their output
①
...
switch (2)
{
default :
printf("s");
break;
case 1:
printf("o");
break;
}
...result :s
②
...
switch (2)
{
default :
printf("s");
case 1:
printf("o");
break;
}
...result :so
③
...
switch (2)
{
default :
printf("s");
break;
case 1:
printf("o");
}
...result :s
The specific reason is as detailed in detail 2 ;
switch-case Special use of
case It can express the range
...
int a;
scanf("%d",&a);
switch (a)
{
case 1 ... 10:
printf("1\n");
break;
case 11 ... 20:
printf("2\n");
break;
case 21 ... 30:
printf("3\n");
break;
default:
printf(" I don't know \n");
break;
}
...In the code snippet above , If input :9, The output :1< enter >;
Input :25, The output :3< enter >;
Input :99, The output : I don't know < enter >;
Be careful :case Can only represent integer range after , In the symbol “...” There must be a space before and after ;
Conclusion : I hope this article is helpful to you , Remember to like the collection
边栏推荐
- 2022 R2 mobile pressure vessel filling test questions and online simulation test
- Based on three JS offshore wind power digital twin 3D effect
- 如何快速查询手机在网状态
- SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍
- Pytorch loading model error resolution
- 【HCIE论述】RR-A
- [matlab] matrix operation
- Distributed lock implementation
- VHDL编程实验练习题合集
- PLC也能制作小游戏----Codesys编写猜数字小游戏
猜你喜欢

2022年危险化学品经营单位安全管理人员考试试题及在线模拟考试

TCP与UDP

基于Three.js海上风电数字孪生三维效果

Tableau

PMP training organization

Enterprise wechat H5_ Authentication, PC website, enterprise wechat scanning code, authorized login

2022 R2 mobile pressure vessel filling test questions and online simulation test

Vscode实现PHP在浏览器实时预览

Pytorch loading model error resolution

Learn to divide subnets in an article
随机推荐
同花顺开证券账户怎么样?到底安不安全呢
2022年R2移动式压力容器充装考试题及在线模拟考试
Start blogging
在 Golang 中构建 CRUD 应用程序
SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍
Software development tools [3] theoretical basis of software development tools
Explanation and practice of implicit transformation and implicit parameters in Scala
leaflet如何加载10万条数据
Is the PMP training organization an actual training?
[hcie discussion] multicast igmp-a
Is the revised PMP worth testing?
VHDL programming experiment exercises collection
2022年危险化学品经营单位安全管理人员考试试题及在线模拟考试
KConfig
Design MySQL table structure for message queue to store information data
Talent Weekly - 5
Interprocess communication - shared memory shmat
Learn to divide subnets in an article
Is divicon still used in leaflet in H5 era?
2022美容师(技师)上岗证题目及答案