freeCodeCamp/guide/chinese/c/for/index.md

1.1 KiB
Raw Blame History

title localeTitle
For Loop 对于循环

对于循环

for循环执行代码块直到指定的条件为false。当迭代次数可变时使用while循环,否则使用for循环。 for循环的常见用途是数组迭代。

For循环的语法

for ( init; condition; increment ) { 
   statement(s); 
 } 

for循环由3个部分组成初始化部分特定条件和增量或减量操作部分。这3个部分控制for循环。

初始化语句只执行一次。然后评估测试表达式。如果测试表达式为假0则for循环终止。但是如果测试表达式为真非零则执行for循环体内的代码并更新更新表达式。重复此过程直到测试表达式为假。

当迭代次数已知时通常使用for循环。

#include <stdio.h> 
 
 int main () { 
 
    int array[] = {1, 2, 3, 4, 5}; 
 
    for (int i = 0; i < 5; i++) { 
        printf("Item on index %d is %d\n", i, array[i]); 
    } 
 } 

输出:

> Item on index 0 is 1 
 > Item on index 1 is 2 
 > Item on index 2 is 3 
 > Item on index 3 is 4