当前位置:网站首页>C language -- 11 branch statement if else
C language -- 11 branch statement if else
2022-06-10 21:35:00 【Try!】
1、 Preface
(1)C Language is a structured programming language .C The three basic program structures of the language are :
- Sequential structure : Follow the program sequence
- Selection structure : Select the branch direction according to the judgment result
- Loop structure : Refers to having a loop body , The number of cycles can be determined according to the judgment conditions
(2) Branch statement ( Select statement ) And loop statements
(3) What is a sentence ?
C There is a semicolon in language (;) Separated by a statement .
2、if The grammatical structure of a sentence
There are roughly three kinds of :
if( expression )
sentence ;
if( expression )
sentence 1;
else
sentence 2;
// Multiple branches
if( expression 1)
sentence 1;
else if( expression 2)
sentence 2;
else
sentence 3;
3、 About if else Examples of statements
Example 1 : understand if else structure
#include <stdio.h>
int main()
{
int age = 10;
if (age >= 18)
printf(" adult \n");
return 0;
}
Run the program , There will be no printouts ; If you add
else
printf(" A minor \n");
Then the running result is :
A minor
Example 2 :if or else By default, only the following sentence is executed
The age in example 1 10 Change it to 20, And then else Add a sentence to the sentence of , View the run results .
#include <stdio.h>
int main()
{
int age = 20;
if (age >= 18)
printf(" adult \n");
else
printf(" A minor \n");
printf(" You can't go to Internet cafes \n");
return 0;
}
Running results :
adult
You can't go to Internet cafes
Obviously, this result is unreasonable , What we want is if “ A minor ”, To print “ You can't go to Internet cafes ”, But the age entered is 20, Show “ adult ” That's all right. , It still shows “ You can't go to Internet cafes ”. This proves if/else The default is to execute only one statement below it . When I type this code , In fact, this compiler is already very smart , Can reflect if/else The default is to execute only one statement below it One manifestation of this property is :else The second statement under it is automatic and else The alignment of the , To see if the program can only print “ adult ”, This code is specially given to else The second item under printf Before the statement tab key .
So how can we make the program realize the function we want ?
take else The two statements to be executed inside are enclosed in curly braces , One “{}” It's just a code block , It is a logic .
{
Statement list ;
}
The code is revised as follows :
#include <stdio.h>
int main()
{
int age = 20;
if (age >= 18)
{
printf(" adult \n");
}
else
{
printf(" A minor \n");
printf(" You can't go to Internet cafes \n");
}
return 0;
}
Example 3 :if else Multi branch case of
The wrong sample :
int main()
{
int age = 60;
if (age < 18)
printf(" juvenile \n");
else if (18 <= age < 26)
printf(" youth \n");
return 0;
}
After running the program , prints “ youth ”. Why does this result ?
So though 60 It's not in 18 To 26 Between , Will also print “ youth ”.
Write it correctly :
int main()
{
int age = 60;
if (age < 18)
printf(" juvenile \n");
else if (age >= 18 && age < 26)
printf(" youth \n");
else if (age >= 26 && age < 40)
printf(" Prime of life \n");
else if (age >= 40 && age < 60)
printf(" Middle and old age \n");
else
printf(" The elderly \n");
return 0;
}
The running result is : The elderly
Example 4 : In the air else
int main()
{
int a = 0;
int b = 2;
if (a == 1)
if (b == 2)
printf("hi\n");
else
printf("hello\n");
return 0;
}
After running the code , No results in the print window . Why? ?
Let's see if I don't delete it manually tab interval , How does the compiler recognize this code .( I'll add to it {} Make the program more readable )
int main()
{
int a = 0;
int b = 2;
if (a == 1)
{
if (b == 2)
{
printf("hi\n");
}
else
{
printf("hello\n");
}
}
return 0;
}
You can see ,else With the one closest to him if Match the , because a The value of is 0, So we won't enter the first if, Naturally, there will be no printed results . So code separation is very important , add {} Separating the code can improve the readability of the program .
4、if else Contrast of writing forms 
Code one and code two actually implement the same function . Let's use a piece of code to explain why code one and code two are actually the same .
int test()
{
if (1)
return 0;
printf("haha\n");
return 1;
}
int main()
{
test();
return 0;
}
When running the program , Find that nothing can be printed . Press the fn+f10 Debugging code , You can see that the execution is finished test Medium return 0 Just skip it printf(“haha\n”);return 1; These two sentences .
Modify the code as follows :
int test()
{
if (0)
return 0;
return 1;
}
int main()
{
test();
return 0;
}
You can see that the code goes to if (0) after , Go on and you will arrive at return 1 了 .

Code three and code four are the same , Why the num == 5 Written as 5 == num Well ? In order to avoid a situation
int main()
{
int num = 1;
if (num = 5)
{
printf("hello\n");
}
}
take == It has been written. =, Will become 5 Assign a value to num, At this time, the judgment condition is true , It's not judgment num Whether it is 5 了 , The program will eventually print hello. But in code 4, if you will 5 == num Written as 5 = num The program reports an error .
5、 Example
Output 1-100 Between the odd numbers
int main()
{
int i;
for(i = 0; i < 100; i++)
if (i % 2 == 1)
{
printf("%d\t", i);
}
else
{
printf("");
}
return 0;
}
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
边栏推荐
- Vissim仿真快速入门
- Arduino中Serial.print()与Serial.write()函数的区别,以及串口通信中十六进制与字符串的收发格式问题和转换过程详解
- H. Relationship among Nalu, RBSP and sodb in 264
- A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
- 分布式服务理论基础
- LeetCode 进阶之路 - 搜索插入位置
- C language ---5 initial string, escape character and comment
- Zero trust architecture
- Factory and strategy mode implementation scheme of coupons
- 北大青鸟昌平校区:高中学历可以学UI吗?
猜你喜欢

微积分复习1

【北大青鸟昌平校区】职教与普教协调发展,今年的中考会容易吗?

Use DAP link to download the executable file separately to the mm32f5 microcontroller

Test APK exception control netlocation attacker development

Construction of RT thread smart win10 64 bit compilation environment

20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology

^29事件循环模型

C language -- 4 first-time constant

Arduino中Serial.print()与Serial.write()函数的区别,以及串口通信中十六进制与字符串的收发格式问题和转换过程详解

Niuke.com: numbers that appear more than half of the times in the array
随机推荐
标配双安全气囊,价格屠夫长安Lumin 4.89万起售
LeetCode 进阶之路 - 反转字符串
app测试用例
Leetcode advanced path - delete duplicates in the sorting array
Nanny tutorial: how to become a contributor to Apache linkis documents
Read the source code of micropyton - add the C extension class module (2)
Software definition boundary (SDP)
C language ---5 initial string, escape character and comment
"O & M youxiaodeng" self service account unlocking tool
CCF class a conference or journal - regression related papers
Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)
Obtained network time + time zone (+8)
Identity and access management (IAM)
Use DAP link to download the executable file separately to the mm32f5 microcontroller
记录一下今天的MySQL故障
蛮力法/邻接表 深度优先 有向带权图 无向带权图
Leetcode divides the array so that the maximum difference is k
Redis缓存击穿
设计多层PCB板需要注意哪些事项?
LeetCode:1037. Effective boomerang - simple