当前位置:网站首页>[C language - zero foundation lesson 8] circular structure and break continue
[C language - zero foundation lesson 8] circular structure and break continue
2022-07-27 09:15:00 【Super Daxiong】
Preface
Blogger :Super Daxiong ( A cute new blogger )
C Language column :0 Basic science C Language column
LeetCode special column :LeetCode special column
This issue is about C Language cycle structure and break continue, If there is any mistake, you are welcome to put forward your views .
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
Catalog
The most important thing in life is circulation , The earth goes around the sun 、 a week 7 God 、 One day 24 Hours 、 An hour 60 Minutes, this is the cycle . stay C There are also cycles in language . If I let you use C Language promises 100 strip “ I like C Language ”, What method would you choose , I don't think you want to play 100 strip print(“ I like C Language \n”); Too tired , And no brain . So the circular structure we learn is used to solve this problem , Loop statements have while、do-while、for.
while() Loop statement
Format
While( Conditions ){
sentence ;
}
conditions for execution : Judge the conditions first , When the condition is true, the loop statement is executed , End the loop until the condition is false .
int i = 1;
while (i<=10)
{
printf("%d",i);
i++;
}result :12345678910
There are three elements of circulation : The initial value of the cycle 、 The loop condition 、 Cycle increment ( Step value )
do-while sentence
Format :do sentence
While( Conditions );
Execution process : Execute statement first , In judging conditions , When the condition is true, continue to execute the loop statement , Until the condition is false .
int i = 1;
do
printf("%d", i);
while (i>5);result :1
int i = 1;
do
printf("%d",i++);
while (i<5);result :1234 i++ The post value usage is used
Do-while and while The difference between loop statements
Do-while Execute the loop at least once ,while The statement may not be executed once .
Do-while The end sign of the loop statement is ;
for() Loop statement
Format
For( Conditions for a : initial value ; Condition 2 : Conditions ; Condition 3 : The incremental ){
sentence ;
}
for ( int i = 0; i <= 100; i++)
{
printf("%d\n",i);
}result : Print out 0-100 Value
Calculation 1-100 Summation of
int sum = 0;
for ( int i = 1; i <= 100; i++)
{
sum += i;
}
printf("1-100 The cumulative sum of is %d",sum);result : 1-100 The cumulative sum of is 5050
For The characteristics of the cycle : It is mostly used to specify the initial value and known termination conditions 、 The number of cycles is controllable
Get into for Loop executes the statement of loop one first ( stay for Only execute once in the loop ), Then judge the conditions , If it's true, it goes into a cycle , Then execute condition 3 next time —— Whether condition two is true, and then in this order , Until the conditions don't hold .
Break and continue
Break sentence
Break sentence : Jump out and terminate the current loop .
No, break The sentence of
for (int i = 1;i <= 5;i++) {
printf("%d\n",i);
}result :
1
2
3
4
5
Yes break The sentence of
stay for In circulation
for (int i = 1;i <= 5;i++) {
printf("%d\n",i);
break;
}result :
1
The program only executes once and jumps out of the loop ;
for (int i = 1;i <= 5;i++) {
if (i == 3) {
break;
}
printf("%d\n",i);
}result :
1
2
The program only executes until the judgment condition is established and directly jumps out for loop ;
stay while In circulation
int i = 1;
while (1) {// Not 0 It's all true , therefore while The condition is always true
if (i > 5) { i>5 sign out while loop
break;
}
printf("%d\n",i++);
}result :
1
2
3
4
5
stay switch In circulation
We can use break Calculate the number of days per month , Because in switch There is no break Just keep going , Until I met break stop it .
int i ;
scanf("%d",&i);
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: printf("31");break;
case 2:
case 4:
case 6:
case 9:
case 11:printf("30");break;
default:
printf("28 or 29");
break;
}continue sentence
continue: End this cycle and proceed to the next cycle
stay for In circulation
for (int i = 0;i <= 5;i++) {
if (i == 4)continue;
printf("%d",i);
}result :01235
stay while In circulation
int i = 0;
while (i++<5)
{
if (i == 4)continue;
printf("%d",i);
}result :1235
continue: Do not use... In selection statements (switch sentence ), Use only in loop statements .
A nested loop
What is nesting : A circular sentence contains another circular sentence , For example, you are watching TV now and the people on the TV screen are watching TV .
example 1:
for (int i = 0; i < 2; i++){
for (int j = 0; j < 1; j++)
{
printf("%d,%d\n", i, j);
}
}result :
0,0
1,0
analysis : We can see the first output 0,0 That is, execute the outer layer first for loop i=0, Then enter the inner layer for loop i=0, Output 0,0 Then the inner layer for loop j+1 be equal to 1,1<1 If the condition is not satisfied, the execution will be executed for loop ,i=1; Then the inner layer for loop j=0 Then the output 1,0 Then the inner layer for The cycle is equal to 1 Exit if the conditions are not met for loop i=2 Exit the loop if the conditions are not met .
example 2:
for (int i = 0; i < 1; i++){
for (int j = 0; j < 5; j++)
{
if (i == 3) {
break;
}
printf("j=%d\n", j);
}
printf("i=%d\n", i);
}result :
j=0
j=1
j=2
j=3
j=4
i=0
break; A statement is to jump out of the nearest for Cyclic .
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
边栏推荐
猜你喜欢

Mangodb simple to use

Intel, squeezed by Samsung and TSMC, finally put down its body to customize chip technology for Chinese chips

BEVFormer: Learning Bird’s-Eye-View Representation from Multi-Camera Images via Spatiotemporal Trans

08_ Service fusing hystrix

flex布局 (实战小米官网)

linux安装和远程连接mysql记录

Explanation of common basic controls for C # form application (suitable for Mengxin)

Antdesign a-modal user-defined instruction realizes dragging and zooming in and out

MySQL transaction

500 error reporting
随机推荐
Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
如何注册码云账号
基于ArkUI eTS开发的坚果笑话(NutJoke)
A survey of robust lidar based 3D object detection methods for autonomous driving paper notes
linux下安装oracle,本地PL/SQL连接Linux下的oracle导入表并新建用户和密码
openharmony萌新贡献指南
Intel, squeezed by Samsung and TSMC, finally put down its body to customize chip technology for Chinese chips
Storage and computing engine
Understand various IOU loss functions in target detection
07_ Service registration and discovery summary
qt中使用sqlite同时打开多个数据库文件
二叉树讲解
[interprocess communication IPC] - semaphore learning
被三星和台积电挤压的Intel终放下身段,为中国芯片定制芯片工艺
ES6 new - deconstruction assignment of array / object
HUAWEI 机试题:火星文计算 js
巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
Nut weather
js call和apply
【ACL2020】一种新颖的成分句法树序列化方法