当前位置:网站首页>Meow, come, come: do you really know if, if else
Meow, come, come: do you really know if, if else
2022-07-07 04:48:00 【Cat star people who love Durian】
Catalog
1.if The implementation steps of
2.if(0) Can be used as a comment
6.if else And if if The difference between
7. adopt “ Print 1000 To 2000 Years between leap years ” Examples of different codes for comparison
Meow blogger today above 7 Let's talk about our if And else:
One 、if The implementation steps of
First, in the C In language ,0 For false , Not 0 It's true .
1. Execute first () The expression in or function , Get true and false results
2. Conditions Decision function
3. Conduct Branch function
Next, let's take a look at the code , What's the print result :
#include<stdio.h>
int main()
{
int a = 0;
if (a = 0)
{
printf("hehe");
}
if (a == 0)
{
printf("haha");
}
return 0;
}
The result is :
Are you right ?
if(a = 0) take 0 Assign a value to a,a = 0 -> if(a) ->if(0), It is false, so it is not executed .
if(a == 0),a Initialize to at the beginning 0, therefore a It is equal to 0, So it's true , Print haha.
Two 、if(0) Can be used as a comment
When the code is not needed , It can be used if(0) notes , Of course, meow bloggers do not recommend taking if(0) notes , Just let everyone see if others use if(0), Know when it's used as a comment .
3、 ... and 、if The wrong way of writing
#include<stdio.h>
int main()
{
int age = 10;
if (18 <= age < 30)
{
printf(" youth \n");
}
return 0;
}
as a result of :18<=age For false =0 < 30 It's true , So to enter if
Four 、else Nearby principle
What is the result of the following code ?
#include<stdio.h>
int main()
{
int a = 0;
int b = 2;
if (a == 1)
if (b == 2)
printf("hehe\n");
else
printf("haha\n");
return 0;
}
result :
Nothing printed .
as a result of :else look for if Nearby principle
Actually : The code is equivalent to
#include<stdio.h>
int main()
{
int a = 0;
int b = 2;
if (a == 1)
{
if (b == 2)
printf("hehe\n");
else
printf("haha\n");
}
return 0;
}
5、 ... and 、else if Two ways of writing
// The first way to write it
#include<stdio.h>
int main()
{
int age = 17;
if (age < 18)
printf(" teenagers \n");
else if (age >= 18 && age < 30)
printf(" youth \n");
else if (age >= 30 && age < 50)
printf(" middle-aged \n");
else
printf(" The elderly \n");
return 0;
}
// The second way
int main()
{
int age = 17;
if (age < 18)
printf(" teenagers \n");
else if ( age < 30)
printf(" youth \n");
else if (age < 50)
printf(" middle-aged \n");
else
printf(" The elderly \n");
return 0;
}
// Meow bloggers recommend the first , Better code readability
6、 ... and 、if else And if if The difference between
Look at the result of the following code :
// The first code
#include<stdio.h>
int main()
{
int age = 17;
if (age < 18)
printf(" teenagers \n");
else if (age >= 16 && age <= 35)
printf(" youth \n");
else if (age >= 30 && age < 50)
printf(" middle-aged \n");
else
printf(" The elderly \n");
return 0;
}
// The second code
#include<stdio.h>
int main()
{
int age = 17;
if (age < 18)
printf(" teenagers \n");
if (age >= 16 && age <= 35)
printf(" youth \n");
if (age >= 30 && age < 50)
printf(" middle-aged \n");
if (age >= 50)
printf(" The elderly \n");
return 0;
}
if else Combination is to make one judgment without making other judgments ;if if Go through each judgment to see whether it is satisfied
7、 ... and 、 adopt “ Print 1000 To 2000 Years between leap years ” Examples of different codes for comparison
How to judge leap years :
1、 Can be 4 to be divisible by , But can't be 100 to be divisible by ;
2、 Can be 400 to be divisible by ;
The following code you think is correct , What is wrong :
//A.
#include<stdio.h>
int main()
{
int y = 0;
for (y = 1000; y <= 2000; y++)
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
printf("%d ", y);
}
}
return 0;
}
//B.
#include<stdio.h>
int main()
{
int y = 0;
for (y = 1000; y <= 2000; y++)
{
if (y % 4 == 0 && y % 100 != 0)
{
printf("%d ", y);
}
if (y % 400 == 0)
{
printf("%d ", y);
}
}
return 0;
}
//C.
#include<stdio.h>
int main()
{
int y = 0;
for (y = 1000; y <= 2000; y++)
{
if (y % 4 == 0)
{
if (y % 100 != 0) {
printf("%d ", y);
}
}
if (y % 400 == 0)
{
printf("%d ", y);
}
}
return 0;
}
//D.
#include<stdio.h>
int main()
{
int y = 0;
for (y = 1000; y <= 2000; y++)
{
if (y % 4 == 0)
{
if (y % 100 != 0) {
printf("%d ", y);
}
}
else if (y % 400 == 0)
{
printf("%d ", y);
}
else
{
;
}
}
return 0;
}
//E.
#include<stdio.h>
int main()
{
int y = 0;
for (y = 1000; y <= 2000; y++)
{
if (y % 4 == 0 && y % 100 != 0)
{
printf("%d ", y);
}
else if (y % 400 == 0)
{
printf("%d ", y);
}
else
;
}
return 0;
}
result :
A、
B、
C、
D、
E、
As shown in the figure D It's wrong. . Meow bloggers use leap years 2000 Explain with chestnuts in
A and B You know , Meow blogger will not explain here ;C:2000 Enter the first if Judge 2000%4==0 Into it if Judge 2000%100==0 So don't print y. because if The characteristic of each if All judgments , So go to the next if Judge ,2000%400==0, So print 2000;D: The explanation is shown in the figure :
;
E: The explanation is shown in the figure :
That's all the content of this blog , Hopefully that helped , If there is a mistake in the text , Just discuss and solve it with the blogger meow , We learn from each other and make progress .
边栏推荐
- [team learning] [34 sessions] Alibaba cloud Tianchi online programming training camp
- Terms used in the Web3 community
- 过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
- 视频融合云平台EasyCVR视频广场左侧栏列表样式优化
- Implementation of JSTL custom function library
- 《原动力 x 云原生正发声 降本增效大讲堂》第三讲——Kubernetes 集群利用率提升实践
- 关于01背包个人的一些理解
- EasyCVR集群版本添加RTSP设备提示服务器ID错误,该如何解决?
- sscanf,sscanf_s及其相关使用方法「建议收藏」
- 浙江大学周亚金:“又破又立”的顶尖安全学者,好奇心驱动的行动派
猜你喜欢
Vscode 如何使用内置浏览器?
用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30
MySQL数据库(基础篇)
[team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
Optimization of channel status offline of other server devices caused by easycvr cluster restart
EasyCVR平台接入RTMP协议,接口调用提示获取录像错误该如何解决?
acwing 843. n-皇后问题
Practice Guide for interface automation testing (middle): what are the interface testing scenarios
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
随机推荐
Complimentary tickets quick grab | industry bigwigs talk about the quality and efficiency of software qecon conference is coming
Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
窗口可不是什么便宜的东西
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
Tiktok may launch an independent grass planting community platform: will it become the second little red book
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
Master the secrets of software security testing methods, and pinch the security test report with your hands
What is JVM? What are the purposes of JVM tuning?
Basic idea of counting and sorting
Vscode automatically adds a semicolon and jumps to the next line
什么是Web3
Digital chemical plant management system based on Virtual Simulation Technology
Terms used in the Web3 community
Nanopineo use development process record
C#使用西门子S7 协议读写PLC DB块
MySQL null value processing and value replacement
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
sscanf,sscanf_s及其相关使用方法「建议收藏」
Data security -- 12 -- Analysis of privacy protection