for loop

来自cppreference.com

 
 
C语言编写
大会的主题
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
预处理器
评论
关键字
ASCII表
转义序列
C
流量控制
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件执行语句
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
迭代语句
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
for loop
跳转语句
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
函数声明
的历史。内嵌说明
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
规范
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV符
存储类说明
alignas说明 (C99)
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
表达式
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
为了评价
其他运营商
运营商
运算符的优先级
实用工具
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
属性 (C99)
转换
杂项
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
内联汇编
 
执行一个循环.....
Original:
Executes a loop.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
.

目录

[编辑] 语法

for ( init_expression ; cond_expression ; iteration_expression ) loop_statement

[编辑] 解释

上面的语法产生的代码等价于:
Original:
The above syntax produces code equivalent to:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
{
init_expression ;
while ( cond_exression ) {
loop_statement
iteration_expression ;
}

}

init_expression执行循环之前执行。 cond_expression应评估值,转换为bool。它是在每次循环迭代计算。该循环继续,仅当它的值是trueloop_statement在每次迭代时被执行,之后执行iteration_expression.
Original:
The init_expression is executed before the execution of the loop. The cond_expression shall evaluate to value, convertible to bool. It is evaluated before each iteration of the loop. The loop continues only if its value is true. The loop_statement is executed on each iteration, after which iteration_expression is executed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
​​可以用作终止声明.
Original:
If the execution of the loop needs to be terminated at some point,
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
可以用作快捷.
Original:
If the execution of the loop needs to be continued at the end of the loop body,
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 关键字

for

[编辑] 为例

下面的示例演示使用的循环中的数组操作
Original:
The following example demonstrates the usage of the for loop in an array manipulation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <stdio.h>
#include <stdlib.h>
 
#define SIZE 8
 
int main (int argc, char **argv)
{
    unsigned i = 0, array [SIZE];
 
    for( ; i < SIZE; ++i)
        array [i] = random() % 2;
 
    printf("Array filled!\n");
 
    for (i = 0; i < SIZE; ++i)
        printf("%d ", array[i]);
 
    printf("\n");
 
    return EXIT_SUCCESS;
}

Output:

Array filled!
1 0 1 1 1 1 0 0