当前位置:网站首页>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 .
边栏推荐
- Field data acquisition and edge calculation scheme of CNC machine tools
- Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
- What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
- VM virtual machine operating system not found and NTLDR is missing
- Comment les tests de logiciels sont - ils effectués sur le site Web? Testez la stratégie!
- [team learning] [34 sessions] Alibaba cloud Tianchi online programming training camp
- Vscode automatically adds a semicolon and jumps to the next line
- 【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
- SQL where multiple field filtering
- mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
猜你喜欢
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
In depth analysis of kubebuilder
C#使用西门子S7 协议读写PLC DB块
Intel David tuhy: the reason for the success of Intel aoten Technology
Programmers go to work fishing, so play high-end!
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
The easycvr platform is connected to the RTMP protocol, and the interface call prompts how to solve the error of obtaining video recording?
EasyCVR平台接入RTMP协议,接口调用提示获取录像错误该如何解决?
随机推荐
Introduction to the PureMVC series
Advertising attribution: how to measure the value of buying volume?
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
Intel David tuhy: the reason for the success of Intel aoten Technology
In depth analysis of kubebuilder
Both primary and secondary equipment numbers are 0
Vscode automatically adds a semicolon and jumps to the next line
[on automation experience] the growth path of automated testing
Mathematical analysis_ Notes_ Chapter 10: integral with parameters
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
System framework of PureMVC
Some understandings about 01 backpacker
The request request is encapsulated in uni app, which is easy to understand
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
acwing 843. N-queen problem
Basic idea of counting and sorting
Practice Guide for interface automation testing (middle): what are the interface testing scenarios
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
Easycvr cannot be played using webrtc. How to solve it?