当前位置:网站首页>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 .
边栏推荐
- DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
- A detailed explanation of head pose estimation [collect good articles]
- Flex layout and usage
- The easycvr platform is connected to the RTMP protocol, and the interface call prompts how to solve the error of obtaining video recording?
- How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
- Both primary and secondary equipment numbers are 0
- On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
- 英特尔David Tuhy:英特尔傲腾技术成功的原因
- Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
- VM virtual machine operating system not found and NTLDR is missing
猜你喜欢

Flex layout and usage

Section 1: (3) logic chip process substrate selection

各路行业大佬称赞的跨架构开发“神器”,你get同款了吗?

Practice Guide for interface automation testing (middle): what are the interface testing scenarios

NTU notes 6422quiz review (1-3 sections)

How to solve the problem of adding RTSP device to easycvr cluster version and prompting server ID error?

窗口可不是什么便宜的东西

DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)

DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)

Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
随机推荐
EasyCVR平台接入RTMP协议,接口调用提示获取录像错误该如何解决?
How does vscade use the built-in browser?
图灵诞辰110周年,智能机器预言成真了吗?
Jetson nano configures pytorch deep learning environment / / to be improved
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
Camera calibration (I): robot hand eye calibration
What work items do programmers hate most in their daily work?
Oracle - views and sequences
R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
Read of shell internal value command
leetcode 53. Maximum subarray maximum subarray sum (medium)
jvm是什么?jvm调优有哪些目的?
Win11玩绝地求生(PUBG)崩溃怎么办?Win11玩绝地求生崩溃解决方法
这项15年前的「超前」技术设计,让CPU在AI推理中大放光彩
食堂用户菜品关系系统(C语言课设)
Basic idea of counting and sorting
Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
Both primary and secondary equipment numbers are 0