当前位置:网站首页>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
边栏推荐
- Unity analyzes the rendering of built-in terrain and does some interesting things
- App test case
- Nodejs: official document 3 Dgram stream
- ^29事件循环模型
- Fast Planner - detailed explanation of kinetic astar
- Zero trust architecture
- In 2021, the average salary will be released, and the IT industry will not be surprised
- Lengsuanling, a 30-year tortuous history of IPO of a domestic brand
- Read the source code of micropyton - add the C extension class module (2)
- Mysql之将查询结果插入到其它表中
猜你喜欢

信号与系统复习1

你的公司会选择开发数据中台吗?

LeetCode:1037. Effective boomerang - simple

Factory and strategy mode implementation scheme of coupons

Redis缓存穿透

Acl2022 | bert2bert: an efficient pre training method of parameter reuse, which significantly reduces the training cost of oversized models

72. editing distance ●●

微积分复习1

Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)

Cas de test app
随机推荐
LeetCode 进阶之路 - 69.X的平方根
Niuke.com: numbers that appear more than half of the times in the array
Read the source code of micropyton - add the C extension class module (1)
Rotate menu 2.0
Redis缓存击穿
Nanny tutorial: how to become a contributor to Apache linkis documents
leetcode 划分数组使最大差为 K
Leetcode advanced path - delete duplicates in the sorting array
Software definition boundary (SDP)
Whether there is a simple path from brute force method /u to V
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated
72. editing distance ●●
^29事件循环模型
Brute force method / task assignment
"O & M youxiaodeng" self service account unlocking tool
^30h5 web worker multithreading
Brute force method / adjacency table depth first directed weighted graph undirected weighted graph
Redis cache breakdown
Leetcode advanced path - the first unique character in a string
Quick start to VISSIM simulation