当前位置:网站首页>[C language series] - branch and loop statements
[C language series] - branch and loop statements
2022-06-29 05:46:00 【Indulge in the evening wind】
Branch and loop statements
One : What is a sentence ?
Two : Branch statement
1.if sentence
2.swltch sentence
3、 ... and : Loop statement
1.while loop
2.for loop
3.do... while loop
Four :goto sentence
One : What is a sentence ?
Two : Branch statement :
1.if sentence :
1.1: Grammatical structure :
if sentence :
1: Single branch :
if( expression )
sentence ;
2. Double branch :
if( expression 1)
sentence 1;
else if( expression 2)
sentence 2;
3. Multiple branches :
if( expression 1)
sentence 1;
else if( expression 2)
sentence 2;
else
sentence 3;
1.2: In the air else:

notes :else The matching of :else It's the closest thing to it if Matching .
2.swltch sentence :
2.1: Grammatical structure :
switch sentence :switch( Integer expression ){Statement item case Integer constant expression :sentence ;}
Code demonstration :
#include <stdio.h>
//switch Code demonstration
int main()
{
int day = 0;
switch(day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("weekday\n");
break;
case 6:
case 7:
printf("weekend\n");
break;
}
return 0;
}notes :break sentence The practical effect is to divide the statement list into different branches .
2.2:default Clause :
3、 ... and : Loop statement :
1:while loop :
1.1: Grammatical structure
while( expression )
Loop statement ;

//break Code instance
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5)
break;
printf("%d ", i);
i = i+1;
}
return 0;
}summary :
break stay while The role in the cycle :
In fact, in the loop, as long as you encounter break, Just stop all the later cycles , Terminate the cycle directly
therefore :while Medium break It's used to terminate the loop permanently
continue:
//continue Code instance 1
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5)
continue;
printf("%d ", i);
i = i+1;
}
return 0;
}//continue Code instance 2
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
i = i+1;
if(i == 5)
continue;
printf("%d ", i);
}
return 0;
}summary :
continue Is used to terminate the cycle , In this cycle continue The following code is no longer executed
It's a jump to while The judgment part of a sentence , Make the entry judgment of the next cycle .
2:for loop :
2.1: Grammatical structure
for ( expression 1; expression 2; expression 3)
Loop statement ;
expression 1expression 1 by Initialization part , Used to initialize loop variables .expression 2expression 2 by Condition judgment part , Used to determine when a loop ends .expression 3expression 3 by Adjustment part , For adjustment of cyclic conditions .

int i = 0;
// The writing method of closing the front and opening the back
for(i=0; i<10; i++)
{}
// Both sides are closed intervals
for(i=0; i<=9; i++)
{}3.do... while loop
3.1:do... while Cyclic syntax format :

3.3:do Characteristics of sentences
#include <stdio.h>
int main()
{
int i = 10;
do
{
printf("%d\n", i);
}while(i<10);
return 0; }Four :goto sentence :
for(...)
for(...)
{
for(...)
{
if(disaster)
goto error;
}
}
…
error:
if(disaster)
// Handling error situations goto sentence An example of a sentence : Shutdown Sequence
#include <stdio.h>
int main()
{
char input[10] = {0};
system("shutdown -s -t 60");
again:
printf(" The computer will be in 1 Shut down in minutes , If input : I am a pig , Just cancel the shutdown !\n Please enter :>");
if(0 == strcmp(input, " I am a pig "))
{
system("shutdown -a");
}
else
{
goto again;
}
return 0; }边栏推荐
- Service grid ASM year end summary: how do end users use the service grid?
- QT precautions and RCC download address
- The win11 file resource manager has an explicit Caton, and Microsoft promises to improve the performance in 2022
- Common methods for describing 3D models of objects and their advantages and disadvantages
- Analysis report on the investment market of the development planning prospect of the recommended rare earth industry research industry in 2022 (the attachment is a link to the online disk, and the rep
- Mongodb basic knowledge summary
- The easiest GUI to compile: dgui
- Design risc-v processor from scratch -- data adventure of five stage pipeline
- AttributeError: module ‘torch. nn. Parameter 'has no attribute' uninitializedparameter 'solution
- 2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin copper (no2tmpp) Cu) /2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin (no2tmpp) H2) Qiyue porphyrin supply
猜你喜欢

Plugin

Regular expressions for shell script values

innography

Design risc-v processor from scratch -- data adventure of five stage pipeline
![Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue](/img/a9/0869c4f39a96cff63d1e310292c46d.jpg)
Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue

In 2022, I haven't found a job yet. I have been unemployed for more than one year. What is the "old tester" for eight years?

Test content

Quickly write MVVM code using source generators

Why Houdini made the pyside2 plug-in crash

HTTP Caching Protocol practice
随机推荐
机器人强化学习——第一人称 VS 第三人称
IDENTITY
Why Houdini made the pyside2 plug-in crash
2022-02 Microsoft vulnerability notification
Conditional test, if and case conditional test statements of shell script
Software architecture final review summary
Mvcc principle in MySQL
The win11 file resource manager has an explicit Caton, and Microsoft promises to improve the performance in 2022
使用VS创建静态链接库.lib并使用
Research Report on recommended specialized, special and new industries in 2022 industry development prospect and market investment analysis (the attachment is a link to the online disk, and the report
Software architecture experiment summary
β- Tetraphenyl nickel porphyrin with all chlorine substitution| β- Thiocyano tetraphenyl porphyrin copper| β- Dihydroxy tetraphenyl porphyrin 𞓜 2-nitroporphyrin | supplied by Qiyue
嵌入式RTOS
Design risc-v processor from scratch -- data adventure of five stage pipeline
[high concurrency] deeply analyze the callable interface
51 lines of code, self-made TX to MySQL software!
HTTP Caching Protocol practice
Research on heuristic intelligent task scheduling
After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
C语言用 printf 打印 《爱心》《火星撞地球》等,不断更新