for 循环笔记

root 站长 2019-05-21 17:47:51 2022-04-11 20:50:55 35

语法格式

for (循环变量赋初值; 循环条件; 循环变量增值) 
{
    语句; // 循环体
    ......
}

例:

for(int i=0; i<100; i++) 
{
     cout<<i;		
}

执行过程 ☆

  • (1) 先执行循环变量赋初值语句。
  • (2) 再判断循环条件,若其值为真(满足条件),则执行循环语句;若其值为假,则退出循环,转到(5)。
  • (3) 执行循环变量增值语句。
  • (4) 转回 (2) 继续执行。
  • (5) 循环结束,执行 for 语句的下一条语句。

流程图

for 流程图.jpg

{{ vote && vote.total.up }}

共 11 条回复

root 站长

for 循环执行过程

for (i = 1; i <= 10; ) cout << 1 << " "; i = i + 1;
for (2 <= 10; ) cout << 2 << " "; 
i = i + 1; //i = 3
for (3 <= 10; ) cout << 3 << " "; 
i = i + 1; //i = 4
for (4 <= 10; ) cout << 4 << " "; 
i = i + 1; //i = 5
......
for (9 <= 10; ) cout << 9 << " "; 
i = i + 1; //i = 10
for (10 <= 10; ) cout << 10 << " "; 
i = i + 1; //i = 11
for (11 <= 10; ) X