当前位置:网站首页>C language -- 14 loop statement for
C language -- 14 loop statement for
2022-06-10 18:15:00 【Try!】
1、 Loop statement for The grammar of
for ( expression 1; expression 2; expression 3)
loop ;
among , expression 1 Is initialization ; expression 2 Is a judgment ; expression 3 It's adjustment .
Example : use for Loop printing 1-10.
int main()
{
int i = 0;
for (i = 1; i < 11; i++)
printf("%d ",i);
return 0;
}
1 2 3 4 5 6 7 8 9 10
There's a point to make , If the judgment part is omitted , It means that the result of judgment is always true , The code goes into an infinite loop . Like the following code : Printing will continue after running haha
int main()
{
for (;;)
{
printf("haha\n");
}
return 0;
}

Add some conditions :
int main()
{
int i = 0;
int j = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("haha\n");
}
}
return 0;
}
The code above will print 9 Time haha, Now omit i=0 as well as j=0 Conditions , Check the running results and you will find Print only three times
int main()
{
int i = 0;
int j = 0;
for (; i < 3; i++)
{
for (; j < 3; j++)
{
printf("haha\n");
}
}
return 0;
}
Because this program is executed first j<3 The three cycles of , After the end of the cycle ,j The value of is 3; here i=1,j=3, No longer satisfied j<3 The condition of the , So no more printing haha
2、for In the loop break as well as continue
stay for There are also... In the loop break as well as continue Of , Their meaning and in while The meaning in the cycle is almost the same , The difference is small .
About break, The code is as follows , The print result is still :1 2 3 4
int main()
{
int i = 0;
for (i = 1; i < 11; i++)
{
if (i == 5)
break;
printf("%d ", i);
}
return 0;
}
About continue, The result is 1-4,6-10( And while In the loop continue The execution results of are different ), Add the break Change it to continue that will do . The operation results are as follows :
1 2 3 4 6 7 8 9 10
The running result shows that 5, Because when i be equal to 5 when , Will skip the printed statement , But after skipping printing, you have to execute i++, So the back 6-10 Will still be printed .for In the cycle continue Will jump to the adjustment section ; and while In the cycle continue Will jump out of the loop .
3、for Some suggestions for loop variable control of statements
- Not in for Modify the loop variable in the loop body , prevent for The cycle is out of control
The following code belongs to changing loop variables in the loop body , This is not good
int main()
{
int i = 0;
int j = 0;
for (i = 0; i < 3; i++)
{
printf("%d ",i);
int j = 0;
for (j = 0; j < 3; j++)
{
printf("haha\n");
}
}
return 0;
}
- Suggest for The loop control variable value of the statement adopts “ Front closing back opening section ” How to write it .
for (i = 1; i < 10; i++)This way of writing is “ Left closed right away ” Section ;for (i = 1; i <= 9; i++)Such a statement has the same meaning as the writing of "left close right open" , But it's not recommended to write . The writing method of "left close and right open" can make the program more readable .
4、for A variation of the cycle
It is also possible to control the loop with two variables .
int main()
{
int x, y;
for (x = 0, y = 0; x < 2 && y < 5; ++x, y++)
{
printf("haha\n");
}
return 0;
}
The run result will print two haha
5、 subject
See how many times the following code will loop ?
int main()
{
int i = 0;
int k = 0;
for (i = 0, k = 0; k = 0; i++, k++)
{
k++;
}
return 0;
}
Running results :
The running result is nothing , Because at the break of the condition k=0 Of is assignment , take 0 Assigned to k,0 For false , This statement here is not a judgment condition , So it's going to cycle 0 Time , Print no results .
边栏推荐
- CUDA实现高效查找--审核未通过?
- Abbexa AML1 DNA 结合 ELISA 试剂盒说明书
- Red vertical left side menu navigation code
- YML file configuration parameter definition dictionary and list
- THE LOTTERY TICKET HYPOTHESIS: FINDING SPARSE, TRAINABLE NEURAL NETWORKS论文笔记
- 搭建在线帮助中心,轻松帮助客户解决问题
- What is image search
- Abbexa 细菌基因组 DNA 试剂盒介绍
- [FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
- Some views on the current CIM (bim+gis) industry
猜你喜欢
随机推荐
4. ssh
XML & XPath parsing
Detailed explanation of MySQL windowing function
Leetcode 875. 爱吃香蕉的珂珂
CUDA Programming (I): add two arrays
AOE network critical path
使用Canvas实现的噪音线条h5js特效
IP summary (tcp/ip volumes 1 and 2)
cocoeval函数使用
正斜杠“/”、反斜杠“\、”转义字符“\”、文件路径分割符傻傻记不清楚
图像搜索是什么
美学心得(第二百三十七集) 罗国正
CodeCraft-22 and Codeforces Round #795 (Div. 2)
QTableWidget / QTableView实用技巧
Abbexa CDAN1 siRNA使用说明书
AI 加持实时互动|ZegoAvatar 面部表情随动技术解析
苹果放大招!这件事干的太漂亮了……
苹果期待的「无密码时代」,真能实现吗?
国货彩妆,败走618
IIS installation and deployment web site









