当前位置:网站首页>First knowledge of loop and branch statements in C language
First knowledge of loop and branch statements in C language
2022-07-22 01:20:00 【Chengdian wuyanzu】
Catalog
One 、 Branch statement (if,Switch)
while in getchar The interpretation of
2、for The basic usage of a sentence
️️ What is a sentence ?a+b; Is a statement ,a; Is a statement , even to the extent that ; It's also a sentence , It's called an empty statement .
C Language sentences are divided into five categories :
1、 Expression statement
2、 Function call statements
3、 Control statement
4、 Coincidence statement
5、 Empty statement
Now let's take a look at the control statement !
One 、 Branch statement (if,Switch)
Branch statements are also called choice statements , It includes two categories , One is if, The other is Switch
1、if The basic usage of
Basic types
if( expression )
sentence ;
Double branch
if( expression )
sentence ;
else
sentence ;
Multiple branches
if( expression )
sentence ;
else if( expression )
sentence ;
...
else
sentence ;
️️ When if When the expression in is true , Get into if, When it is false , Unable to enter . What is true and false ? When the expression is 0 when , Is false , Not 0 It's true , That is, when the expression is correct, it is right or wrong 0, The wrong time is 0. also , When the value represented is 0 when , It's also false , For the wrong 0 When , It's true . This is true or false , We need our own understanding .
When all conditions for branch entry are not met , entering else Branch , Conduct else The content of the branch
In the air else
️️ When if We need to use { }, To use code blocks , Otherwise, it will cause suspension else, Only the contents of the first statement will be read , It is inconvenient for us to judge the operation of the program . Let's take an example
#include<stdio.>
int main()
{
int a=0;
int b=2;
if(a==1)
if(b==2)
printf("hehe\n");
else
printf("haha\n");
return;
}
️️ What do you think the answer is ? Do you think it is hehe, In fact, the answer is empty . Because you can't enter the cycle at all ,else It's a close match , So he and the second if matching , And the second one. if At the first if Inside of , first if You can't get in at all . This shows the importance of format and good compiler . If we add { }, Is it easier to judge , And a good compiler , It will help you read the code in advance , Thus typesetting and the second if alignment , Will let you find your mistakes , Improve readability .
2、Switch The basic usage of
Switch Generally used for multi branch , And branch point When the condition is generally unique Use , At this time, compared with if It is more convenient
switch( Integer expression )
{
case Integral constant expression :
sentence ;
}
️️ When the integer constant is equal to the value expressed by the integer , From this case Start execution Switch What's in the sentence ; It is worth noting that , without break, Will always be executed downward in order , Until I met break Just jump out Switch sentence
for example
switch(day)
{
case 1:
case 2:
case 3:
case 4:
case 5:printf(“ Working day ”);break;
case 6:
case 7:printf(“ Rest Day ”);break;
default: printf(“ Input error ”);break;
}
** In this case , When day by 12345 When any statement in , Will get “ Working day ” Output ,67 Empathy .
and default It is , Not for any case In the case of , Enter this statement , And else Statements like ; among case And default The positions of can be freely exchanged ,default Not necessarily last .
Finally, I would like to remind you , It can be used according to the situation break To the end Switch sentence , Not necessarily every one adds , How convenient , How to use it? .
Two 、 Loop statement
1、while The basic usage of
while( expression )
{
Loop statement ;
}
while Statement is the most basic structure in the loop structure . When the expression is true ( Not 0) Into the circulatory body , When it is false ( by 0) Exit the loop or do not enter the loop body . Other structures can be nested in the recycling body
while Medium break problem
#include<stdio.h>
int main()
{
int i=1;
while(i<10)
{
if(i==5)
break;
printf("%d ",i);
i++;
}
return 0;
}
The output value here is 1 2 3 4. Because when i=5 when , Get into if sentence , encounter break, Just jump out of the loop , No more cycles
while Medium continue problem
#include<stdio.h>
int main()
{
int i=1;
while(i<10)
{
if(i==5)
continue;
printf("%d ",i);
i++;
}
return 0;
}
The output value here is 1 2 3 4 Infinite loop ,continue Its use is , When you meet continue when , Jump to the beginning of the loop , Before continue The part of is not used . summary ,continue To terminate this cycle , That is to say continue The following part is not executed
while in getchar The interpretation of
#include<stdio.h>
int main(){
int ch=0;
while((ch=getchar())!=EOF)
putchar();
return 0;
}
️️getchar For reading characters , But the return value is integer , therefore ch use int To define . The loop here can always read what we enter , Until input Ctrl+z end .EOF Represent in the computer -1
2、for The basic usage of a sentence
for( expression 1; expression 2; expression 3){
The loop body ;
}
️️ Where the expression 1 Bit initialization section , Used to initialize loop variables . expression 2 For the conditional judgment part , amount to while() Part of . expression 3 Bit adjustment part , For adjustment of cyclic conditions , Of course, it can also be used for other conditions , But not recommended
A simple example
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++) // Empty statements can be used here , But it will fall into an infinite cycle
{
printf(“%d”,i);
}
return 0;
}
Printed here 1~10, So let's see while And for Comparison of .
int i=0;
i=1;
while(i<=10)
{
printf("hehe\n");
i++;
}
for(int i=1;i<=10;i++)
{
printf("hehe\n");
}
️️ We can see ,for The amount of code in the loop is larger than while Fewer cycles . in fact ,while What the cycle can do ,for Cycle can do , And more concise , But because a lot of source code has been used before while loop , Will be while The loop retains
Again , stay for I met break Will exit the loop , encounter continue Will terminate this cycle , Jump to expression 2 Make cycle judgment
for Some variants of the cycle use
Code 1
for(;;){
printf(“ ha-ha ”);
}
//for All parts of can be omitted
Code 2
int i=0;
int j=0;
for(i=0;i<10;i++){
for(j=0;j<10;j++){
printf(""hehe");
}
}
Code 3
int i=0;
int j=0;
for(;i<10;i++){
for(;j<10;j++){
printf(""hehe");
}
}
** Code 3 How many do you think hehe Well ? Only 10 individual , This is particularly easy to be wrong , Because every time an outer cycle is carried out , In the inner loop j Not redefined as 0, The boundary has been reached and maintained 10, Will not enter the inner circulation
3、do while The basic usage of
️️do while Circulation and while Circulation similar , however do while The content of the loop body will be carried out once before deciding whether to continue the loop , Here is the basic usage
do{
The loop body
}while( expression );
do-while Loop is the least commonly used of the three loops , Its break and continue In the same way
3、 ... and 、 summary
1、 The use of code must have strict format requirements , It is convenient for others and yourself to read
2、 Basically good code format , Try to use {} To read block code , Even if the {} There is only one statement in
3、for Circulatory body is very important , The initialization part cannot be forgotten , The judgment condition of cycle , Try to close the front and open the back , Easy to read , Like an array , We can know how many units he has according to the conditions , This makes the condition meaningful
4、 Not mentioned above ,scanf When you meet ‘\n’ ‘ ’ when , Will stop reading , If we just want the front part , We can use while(getchar()!='\n') Read out the useless parts later . Avoid mistakes when reading other useful information later
边栏推荐
猜你喜欢
查看IAR工程所用版本号
一种新的UI测试方法:视觉感知测试
45:第四章:开发文件服务:6:第三方云存储解决方案【阿里云OSS】;(购买OSS服务;开通服务;创建一个Bucket;)
The classification of artificial neural network includes: the classification of artificial neural network includes
11 ways to obtain SSL certificates for free
freeRTOS — 任务堆栈使用情况的检测方法
Cloud Foundry 4:应用程序的生命周期
Diffusion Model
【安全狗漏洞通告】Apache Spark 命令注入漏洞方案
OpenGL渲染管道
随机推荐
uniapp 引入腾讯地图
KY novel collection rules (5 collection rules)
软件测试需要学习什么?
Mysql03(关联查询)
Installation du gestionnaire de loisirs
vs2022 快捷键设置
Mysql05(视图)
Interpreting the maker education model in line with the mainstream of the new era
C#托管资源和非托管资源
LeetCode 1911. 最大子序列交替和
(pytorch advanced road VII) attention based seq2seq
Vscode running C language file
LeetCode 1928. 规定时间内到达终点的最小花费
学会自动化必备工具-Selenium-再想着入坑自动化测试吧
Leetcode skimming: related topics of binary tree sequence traversal
Check the version number of IAR project
使用vscode时Tab键缩进从4个空格变为小箭头(已解决)
Leetcode sword finger offer II 061 And the minimum K number pairs***
Carefree Manager installation
Statistics for each month of the year