for 语句 (C)for Statement (C)

使用 for 语句可以重复语句或复合语句指定的次数。The for statement lets you repeat a statement or compound statement a specified number of times. 在可选条件变为 false 前,for 语句的主体将执行零次或多次。The body of a for statement is executed zero or more times until an optional condition becomes false. 可以在 for 语句中使用可选表达式,以便在 for 语句的执行期间初始化和更改值。You can use optional expressions within the for statement to initialize and change values during the for statement's execution.

语法Syntax

iteration-statement:iteration-statement:
  for ( init-expressionopt ; cond-expressionopt ; loop-expressionopt ) statement  for ( init-expressionopt ; cond-expressionopt ; loop-expressionopt ) statement

for 语句的执行将按以下方式继续:Execution of a for statement proceeds as follows:

  1. 将计算 init-expression(如果有)。The init-expression, if any, is evaluated. 这将为循环指定初始化。This specifies the initialization for the loop. init-expression 的类型没有限制。There is no restriction on the type of init-expression.

  2. 将计算 cond-expression(如果有)。The cond-expression, if any, is evaluated. 此表达式必须具有算法或指针类型。This expression must have arithmetic or pointer type. 它在每次迭代前计算。It is evaluated before each iteration. 可能有三个结果:Three results are possible:

    • 如果 cond-expressiontrue(非零),则执行语句,然后计算 loop-expression(若有)。If cond-expression is true (nonzero), statement is executed; then loop-expression, if any, is evaluated. 在每次迭代之后,将计算 loop-expressionThe loop-expression is evaluated after each iteration. 对其类型没有限制。There is no restriction on its type. 副作用将按顺序执行。Side effects will execute in order. 该过程随后从计算 cond-expression 重新开始。The process then begins again with the evaluation of cond-expression.

    • 如果省略了 cond-expression,则 cond-expression 被视为 true,执行将完全按上一段中所述方式继续。If cond-expression is omitted, cond-expression is considered true, and execution proceeds exactly as described in the previous paragraph. 仅当执行了语句体中的 breakreturn 语句时,或执行了 goto(到 for 语句体外的标记语句)时,没有 cond-expression 参数的 for 语句才会终止。A for statement without a cond-expression argument terminates only when a break or return statement within the statement body is executed, or when a goto (to a labeled statement outside the for statement body) is executed.

    • 如果 cond-expressionfalse (0),,则 for 语句的执行将终止,控制权将传递到程序中的下一条语句。If cond-expression is false (0), execution of the for statement terminates and control passes to the next statement in the program.

    for 语句还会在语句体中执行 breakgotoreturn 语句时终止。A for statement also terminates when a break, goto, or return statement within the statement body is executed. for 循环中的 continue 语句会导致计算 loop-expressionA continue statement in a for loop causes loop-expression to be evaluated. 当在 for 循环中执行 break 语句时,不会计算或执行 loop-expressionWhen a break statement is executed inside a for loop, loop-expression is not evaluated or executed. 以下语句This statement

for( ;; )  

是生成只能与 breakgotoreturn 语句一起退出的无限循环的习惯方法。is the customary way to produce an infinite loop which can only be exited with a break, goto, or return statement.

代码Code

以下示例演示了 for 语句:This example illustrates the for statement:

// c_for.c  
int main()  
{  
   char* line = "H e  \tl\tlo World\0";  
   int space = 0;  
   int tab = 0;  
   int i;  
   int max = strlen(line);  
   for (i = 0; i < max; i++ )   
   {  
      if ( line[i] == ' ' )  
      {  
          space++;  
      }  
      if ( line[i] == '\t' )  
      {  
          tab++;  
      }  
   }  

   printf("Number of spaces: %i\n", space);  
   printf("Number of tabs: %i\n", tab);  
   return 0;  
}  

输出Output

Number of spaces: 4  
Number of tabs: 2  

请参阅See Also

语句Statements