当前位置:网站首页>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
边栏推荐
- Leetcode divides the array so that the maximum difference is k
- ^30h5 web worker multithreading
- Unity analyzes the rendering of built-in terrain and does some interesting things
- C language ---5 initial string, escape character and comment
- Full Permutation V3 recursion of brute force method /1~n
- LeetCode 进阶之路 - 加一
- Identity and access management (IAM)
- Asynchronous, thread pool (completablefuture)
- H. Relationship among Nalu, RBSP and sodb in 264
- Codeforces Round #798 (Div. 2)
猜你喜欢

Self attention and multi head attention

CET-6 - Business English - the last recitation before the test

面试必备——synchronized底层原理的基础知识

学IT毕业后该去哪个城市?哪个岗位薪资高?哪些公司待遇好?

登堂入室之soc开发环境及硬件开发准备

Lengsuanling, a 30-year tortuous history of IPO of a domestic brand

信号与系统复习1

一、Vulkan开发理论基础知识

^29事件循环模型

数据库系统概论 ---- 第一章 -- 绪论(重要知识点)
随机推荐
Fast Planner - detailed explanation of kinetic astar
What is the difference between localhost and 127.0.0.1?
H. Relationship among Nalu, RBSP and sodb in 264
LeetCode 进阶之路 - 反转字符串
分布式服务理论基础
Leetcode advanced path - Search insertion location
LeetCode:1037. Effective boomerang - simple
CET-6 - Business English - the last recitation before the test
Test APK exception control netlocation attacker development
C language ---5 initial string, escape character and comment
app測試用例
Heap sorting and hardening heap code for memory
App test case
Redis缓存雪崩
Talk about server performance optimization ~ (recommended Collection)
设计多层PCB板需要注意哪些事项?
Identity and access management (IAM)
Serial Print() and serial The difference of write() function, and the problem of hexadecimal and string sending and receiving format in serial port communication and detailed explanation of the conver
保姆级教程:如何成为Apache Linkis文档贡献者
Leetcode advanced path - delete duplicates in the sorting array