当前位置:网站首页>[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; }边栏推荐
- Redis notes (II) operating commands for keys in redis
- Robot reinforcement learning -- first person vs third person
- Mongodb basic knowledge summary
- 2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up
- 64 commonly used terms for data analysis, really all!
- real time AI based system questionaires
- The translation of those exquisite lines in the eighth season of the big bang
- PCI Verilog IP
- Difference between parametric continuity and geometric continuity
- How to choose congestion model and anemia model
猜你喜欢

Problems with MySQL database query

2022 recommended REITs Industry Research Report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously updated)

Open source demo| you draw and I guess -- make your life more interesting

Ctrip launched the "3+2" office mode. Are you sour?

IDENTITY

2022 recommended high-speed rail industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously

VLAN experiment

2022 recommended RCEP regional comprehensive economic partnership agreement market quotation Investment Analysis Industry Research Report (the attachment is a link to the online disk, and the report i

HTTP Caching Protocol practice

Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin
随机推荐
使用VS创建静态链接库.lib并使用
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?
Satellite navigation time service related terms Collection Edition
Kubernetes backup disaster recovery service product experience tutorial
[high concurrency] deeply analyze the callable interface
ICLR is going to have a big discussion on the deep generation model. Max welling and the winner of the AAAI million dollar award are here. Bengio is one of the organizers
D Author: import C programming in D
JS messagechannel transport
Skills of writing test cases efficiently
See how I do it step by step (I)
Robot reinforcement learning -- first person vs third person
5- (4-benzoimide phenyl) - 10,15,20-triphenylporphyrin (battph2) and its Zn complex (battpzn) / tetra (4-aminophenyl) porphyrin (tapph2) Qiyue supply
Continue yesterday's plan: February 16, 2022
2022 recommended tire industry research report industry development prospect market analysis white paper
证券开户安全么,有没有什么危险呢
Output various graphics and text on the console through C #
The win11 file resource manager has an explicit Caton, and Microsoft promises to improve the performance in 2022
Use typescript compiler parameter 'skiplibcheck' - usage of the typescript compiler argument'skiplibcheck'
[chromium] win10 vs2019 environment chromium configuration and compilation.
data management plan