当前位置:网站首页>Detailed explanation of C language branch statements
Detailed explanation of C language branch statements
2022-07-05 15:26:00 【Houli crab 670】
if else
- if else Use
such as “ When :18<age<45” when , Output “ middle-aged person ”, Output in other cases “ error ”.
if(18<age&&45>age){
printf(" middle-aged person ");
}else{
printf(" error ");
}
Like this if else Use ,if Put conditions in brackets after , If the condition is true, output the following statement , Otherwise output else The sentence of .
Be careful “18<age<45” Writing in brackets is not direct “18<age<45”, Instead, separate them with logical statements , Otherwise, the result will be like this
No matter what number is output, the answer is middle-aged , Because I will judge first “18<age”, Then there are only two cases of this expression anyway , Only 1 and 0( It's 1, Fake is 0), And the result and 45 Compare the size , It's always 45 Big , The condition is always true , So always export middle-aged people .
- if else Just follow the next sentence
Not nested
if(0==a)
printf("CSDN");
else
printf("CS");
This is to output only the following statement , If there are many sentences, add {}, Make them a compound statement
if(0==a){
printf("CSDN");
a++;
}
else{
printf("CS");
a++;
}
nesting
if(0==a){
printf("CSDN");
} else if(1==a){
printf("CSDN");
}else{
printf("CS");
}
ad locum ,else if Count as a sentence , Because it only outputs two results .
- else Always with the nearest if matching
int a = 0;
if(0==a)
if(1==a)
printf("CSDN");
else
printf("CS");
printf("CSGO");
Its result is “CSGO”, because else It's the nearest if Matching , That is to say 1==a the ( I deliberately put else Put it in the second if Below ), So I suggest writing every time if else Use curly braces to enclose , In this way, mistakes can be avoided , Second, it can make the program look more beautiful .( There will be a code style article in the future )
switch
Talking about switch Before , Let's talk first. break and default.
break
break It means to break ( Permanent termination ) loop , It can also be understood as jumping out of this cycle .
We will talk about it in more detail later , More flexible .
default
default Means to go out , Non performance means . stay switch in , If except case Other than that Then output default Subsequent statements .
case Decide the entrance ,break Decide to export .( This sentence is useful later )
- switch Use
switch( Integer expression ){
Statement item :
}
Statement items are just some case sentence , such as :
switch(day){
case 1:
printf(" Monday ");
case 2:
printf(" Tuesday ");
}
Simply put
case Integer expression :
sentence ;
Now take a complete example
This is a code to input the number and output the corresponding week , without break It would be like this
So I just answer the above words
case Decide the entrance ,break Decide to export .
But not everyone case You need to add break, such as ( This is to input the number to determine whether it corresponds to a working day or a rest day )
This is to break Flexible use , Instead of blindly adding break.
Suggest : At the end of the default It's better to add break The habit of , Because if you or other programmers want to default If you add other sentences after it , Then there may be repeated output .
- switch The nested use of
In fact, nested use is easy , Directly above
There's no point , Hit at random , Take a look and you will know how to use nesting .
边栏推荐
- Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
- mapper. Comments in XML files
- Ctfshow web entry explosion
- 把 ”中台“ 的思想迁移到代码中去
- 复现Thinkphp 2.x 任意代码执行漏洞
- 如何将 DevSecOps 引入企业?
- I spring web upload
- Nine hours, nine people, nine doors problem solving Report
- ionic cordova项目修改插件
- Severlet learning foundation
猜你喜欢
Common redis data types and application scenarios
I spring and autumn blasting-2
OSI 七层模型
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
Fr exercise topic - simple question
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
爱可可AI前沿推介(7.5)
OSI seven layer model
Transfer the idea of "Zhongtai" to the code
随机推荐
1330:【例8.3】最少步数
Install PHP extension spoole
Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
ICML 2022 | 探索语言模型的最佳架构和训练方法
R 熵权法计算权重及综合得分
Super wow fast row, you are worth learning!
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
sql server学习笔记
可转债打新在哪里操作开户是更安全可靠的呢
华为哈勃化身硬科技IPO收割机
Detailed explanation of QT creator breakpoint debugger
String modification problem solving Report
把 ”中台“ 的思想迁移到代码中去
CODING DevSecOps 助力金融企业跑出数字加速度
Ecotone technology has passed ISO27001 and iso21434 safety management system certification
Anti shake and throttling
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
JS topic - console log()
Want to ask the big guy, is there any synchronization from Tencent cloud Mysql to other places? Binlog saved by Tencent cloud MySQL on cos
B站做短视频,学抖音死,学YouTube生?