当前位置:网站首页>Elementary level of C language -- while, for, do while
Elementary level of C language -- while, for, do while
2022-07-28 04:23:00 【yin_ Yin】
List of articles
There are some things in life that we need to repeat 、 Do it over and over again , At this time, we can say that we cycle to do this .

To learn circular statements , First, let's introduce what loop in programming language means :
Loop is a kind of computer process in which some code is executed repeatedly in programming language , The common ones are cycle by number and cycle by condition .
In many practical problems, there are many repetitive operations with regularity , Therefore, some statements need to be executed repeatedly in the program . A set of statements that are repeatedly executed is called a loop body , Can you continue to repeat , Depending on the termination condition of the loop . Loop statement is composed of loop body and loop termination condition .
Then let's introduce C In language 3 In the loop :
One .while loop
We have mastered ,if sentence :
if( Conditions )
sentence ;
When the conditions are met ,if Statement execution after statement , Otherwise, do not execute .
But this statement will only be executed once .
Because we find that many practical examples in life are : We need to finish the same thing many times .
So how do we do it ?
C Language introduces us : while sentence , It's possible to loop .
1. Grammar introduction and basic use
First, let's learn while loop , What is while Circulation? ?
We know ,while Be worthy of … When the meaning of , therefore while The cycle is When a specific condition is met, the loop body is executed , Once you are not satisfied , Just close the loop .
while The grammatical structure of :
//while Grammatical structure
while ( expression )
Loop statement ;

for instance , We want to print numbers on the screen 1——10, You can use while loop :
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
printf("%d ", i);
i = i + 1;
}
return 0;
}

When i Value added to 10 When , Satisfy i<=10, Execute the loop again ,i add 1, Turn into 11, Then judge , dissatisfaction i<=10, The loop ends .
The above code has helped me understand while Basic syntax of statements , Then let's continue to learn further :
2. while In the loop break The role of
break Yes End , interrupt , get away It means , So in the loop break What is the function of ?
We learn through a piece of code break The role of :
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
if (i == 5)
break;
printf("%d ", i);
i = i + 1;
}
return 0;
}
Let's think about it , What is the output result :
The answer is Yes !!!, In the loop break The cycle is over .
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 .
3.while In circulation continue The role of
It introduces break In while The role of , Let's introduce it again continue Again while The role in the cycle :
Let's explain it through a few examples , Code up :
Let's look at the first one :
//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;
}
Think about it , What's the result ?
Why is this happening ? Don't worry. , Let's look at another code :
//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;
}
Let's first look at the difference between this code and the previous one , Think again about the result :
We found that , This code is compared with the previous one , Just put i=i+1 This code is placed in if Before the statement , What difference will the result make ?
Is that right ? Yes indeed .
Now we can explain the result of the last code well :
summary :
continue stay while The role of the loop is :
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 
Two .for loop
1. Grammar introduction and basic use
We already know that while loop , But why should we have one for Circulation? ?
So let's see first for Circular grammar :
for( expression 1; expression 2; expression 3)
Loop statement ;

Look at a practical problem :
Use for loop Print... On the screen 1-10 The number of
#include <stdio.h>
int main()
{
int i = 0;
//for(i=1/* initialization */; i<=10/* Judgment part */; i++/* Adjustment part */)
for(i=1; i<=10; i++)
{
printf("%d ", i);
}
return 0; }
Let's see what the result is :
I believe that now we are interested in for The basic use of loops has been understood .
2.for Circulation and while Contrast of cycles
We use for Circulation and while Loop implements the same function , Make a comparison :
To achieve the same function , Use while
int i = 0;
i=1;// Initialization part
while(i<=10)// Judgment part
{
printf("hehe\n");
i = i+1;// Adjustment part
}
To achieve the same function , Use for
for(i=1; i<=10; i++) {
printf("hehe\n");
}
It can be found in while There are still three necessary conditions for a cycle , But due to the problem of style The three parts are likely to deviate far , such The search and modification is not centralized and convenient enough .
therefore ,for The circular style is better ;for The frequency of recycling is also the highest .
3. break and continue stay for The role in the cycle
stay for There can also be... In the loop break and continue, Their meaning and in while It's the same in the loop .
Code 1:
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 5)
break;
printf("%d ", i);
}
return 0;
}

Code 2:
// Code 2
#include <stdio.h>
int main()
{
int i = 0;
for(i=1; i<=10; i++)
{
if(i == 5)
continue;
printf("%d ",i);
}
return 0; }

When it's better, we are for Write this in the loop Don't like while That's a dead cycle . because continue The adjustment part cannot be skipped
So in for In circulation ,break and Continue The same is true of :
1. encounter break, Just stop all the later cycles , Terminate the cycle directly , Execute the following part of the loop .
2. encounter continue, Skip directly to the adjustment section , And then make a conditional judgment .
4.for Statement loop control variable
Here are some suggestions :
1. Don't be in for Modify the loop variable in the loop body , prevent for The cycle is out of control .
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
i = 3;
if (i == 5)
continue;
printf("%d ", i);
}
return 0;
}

2. Suggest for The loop control variable value of the statement adopts “ Front closing back opening section ” How to write it .( Just suggest , If it is inappropriate to write in this way, there is no need to force )
5. some for A variation of the cycle
for The initialization part of the loop , Judgment part , The adjustment part can be omitted , However, it is not recommended to omit , Easy to cause problems
1.
for instance :
#include <stdio.h>
int main()
{
// Code 1
for (;;)
{
printf("hehe\n");
}
}
Dead cycle , Why? ?
2. Here's an example :
#include <stdio.h>
int main()
{
// Code 2
int i = 0;
int j = 0;
int count = 0;
// How many are printed here hehe?
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
count++;
printf("hehe\n");
}
}
printf("count=%d\n", count);
}
How many times to print hehe Well ?
This is easier , Print 100 Time .
That's still this code , If Omit the initialization part , How many are printed here hehe?
// Code 3
int i = 0;
int j = 0;
// If the initialization part is omitted , How many are printed here hehe?
for(; i<10; i++)
{
for(; j<10; j++)
{
printf("hehe\n");
}
}

After removing the initialization part , Value printing 10 Time :
3.for There can be multiple loop control variables in a loop
for instance :
#include <stdio.h>
int main()
{
// Code 4- Use multiple variables to control the loop
int x, y;
for (x = 0, y = 0; x < 2 && y < 5; ++x, y++)
{
printf("hehe\n");
}
return 0;
}

Okay ,for The cycle is over .
3、 ... and .do while loop
Next up do while loop
1. Grammar introduction and basic use
do
Loop statement ;
while ( expression );

characteristic :
Loop at least once , The scenarios used are limited , So I don't often use .
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d ", i);
i=i+1;
}while(i<=10);
return 0; }

2. break and continue stay do while The role in the cycle
break and continue stay do while The role of circulation is also in while The same in the loop .
Show me :
Code 1:
#include <stdio.h>
int main()
{
int i = 1;
do
{
if(5 == i)
break;
printf("%d ", i);
i=i+1;
}while(i<=10);
return 0; }

Code 2:
#include <stdio.h>
int main()
{
int i = 1;
do
{
if(5 == i)
continue;
printf("%d ", i);
i=i+1;
}while(i<=10);
return 0; }

That's right C The introduction of circular statements in language .
Welcome to correct !!!
边栏推荐
- [performance optimization methodology series] III. core idea of performance optimization (2)
- The simulation test disconnects the server from the public network
- 金仓数据库KingbaseES安全指南--5.2. 数据完整性保护
- Kingbasees Security Guide for Jincang database -- 4 data access protection
- Thoroughly understand the sharing function in wechat games
- 20-Openwrt crond crontab
- ESP8266 WIFI 模块和手机通信
- Notes to subject 2
- MATLB | location and constant volume IEEE30 node implementation of distributed energy
- Kingbasees Security Guide for Jincang database -- 5.1. database transmission security
猜你喜欢

H. 265 web player easyplayer realizes webrtc video real-time recording function

xml文件使用及解析

null安全与异常

空间复杂度计算超全整理!!(一起手撕复杂度计算

Cookies and session

Go结构体

Null security and exception

un7.27:redis数据库常用命令。

40: Chapter 4: Development File Service: 1:fastdfs: (1): introduction to fastdfs;

重要的 SQL Server 函数 - 字符串实用程序
随机推荐
26 openwrt port forwarding DMZ UPnP
Use Baidu developer tool 4.0 to build a dedicated applet IDE
idea启动项目mvn命令终端用不了法将“mvn”项识别为 cmdlet
ftp服务器、nfs服务器的搭建和使用
The State Administration of market supervision exposes typical cases of food safety
Work fishing clock simulator wechat applet source code
Istio's Traffic Management API
Fearless of side impact damage, Chery arize 8 fully protects the safety of passengers
Regression - linear regression
Kingbasees Security Guide for Jincang database -- 4 data access protection
【伸手党福利】微信中h5网页调起扫一扫最简单的方法
H. 265 web player easyplayer realizes webrtc video real-time recording function
idea2022更改本地仓库,配置阿里云中央仓库
[untitled]
Machine learning 06: Decision Tree Learning
Go结构体
C语言初阶——循环语句(while,for,do while)
Kingbasees Security Guide for Jincang database -- 5.2. data integrity protection
Construction and use of FTP server and NFS server
网页源代码查看竟然有这么多方法!你都知道吗?