当前位置:网站首页>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 .
边栏推荐
- 这项15年前的「超前」技术设计,让CPU在AI推理中大放光彩
- Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
- JetBrain Pycharm的一系列快捷键
- leetcode 53. Maximum Subarray 最大子数组和(中等)
- [line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
- Terms used in the Web3 community
- [digital analog] source code of MATLAB allcycles() function (not available before 2021a)
- 计数排序基础思路
- Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
- Unit test asp Net MVC 4 Application - unit testing asp Net MVC 4 apps thoroughly
猜你喜欢

Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30

Introduction to the PureMVC series

DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
![[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet](/img/58/4337f0972f7171a5c21e640f03e0b7.png)
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet

What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash

英特尔David Tuhy:英特尔傲腾技术成功的原因

Mathematical analysis_ Notes_ Chapter 10: integral with parameters

What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures

英特尔与信步科技共同打造机器视觉开发套件,协力推动工业智能化转型

Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
随机推荐
树与图的深度优先遍历模版原理
Win11远程桌面连接怎么打开?Win11远程桌面连接的五种方法
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
MySQL forgot how to change the password
Basic idea of counting and sorting
ESG Global Leaders Summit | Intel Wang Rui: coping with global climate challenges with the power of science and technology
掌握软件安全测试方法秘笈,安全测试报告信手捏来
浙江大学周亚金:“又破又立”的顶尖安全学者,好奇心驱动的行动派
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
[team learning] [phase 34] Baidu PaddlePaddle AI talent Creation Camp
ACL2022 | 分解的元学习小样本命名实体识别
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
EasyCVR集群版本添加RTSP设备提示服务器ID错误,该如何解决?
In depth analysis of kubebuilder
C # use Siemens S7 protocol to read and write PLC DB block
Thesis landing strategy | how to get started quickly in academic thesis writing
Both primary and secondary equipment numbers are 0
R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
这项15年前的「超前」技术设计,让CPU在AI推理中大放光彩
Common methods of list and map