Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      for loop

      From cppreference.com
      <c‎ |language
       
       
       
      Statements
      Labels
      Expression statements
      Compound statements
      Selection statements
      Iteration statements
      for
      Jump statements
       

      Executes a loop.

      Used as a shorter equivalent ofwhile loop.

      Contents

      [edit]Syntax

      attr-spec-seq(since C23)(optional)for(init-clause;cond-expression;iteration-expression)loop-statement

      [edit]Explanation

      Behaves as follows:

      • init-clause may be an expressionor a declaration(since C99).
      • Aninit-clause, which is an expression, is evaluated once, before the first evaluation ofcond-expression and its result is discarded.
      • Aninit-clause, which is a declaration, is in scope in the entire loop body, including the remainder ofinit-clause, the entirecond-expression, the entireiteration-expression and the entireloop-statement. Onlyauto andregisterstorage class specifiers are allowed for the variables declared in this declaration.
      (since C99)
      • cond-expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.
      • iteration-expression is evaluated after the loop body and its result is discarded. After evaluatingiteration-expression, control is transferred tocond-expression.

      init-clause,cond-expression, anditeration-expression are all optional. Ifcond-expression is omitted, it is replaced with a non-zero integer constant, which makes the loop endless:

      for(;;){printf("endless loop!");}

      loop-statement is not optional, but it may be a null statement:

      for(int n=0; n<10;++n,printf("%d\n", n));// null statement

      If the execution of the loop needs to be terminated at some point, a break statement can be used anywhere within theloop-statement.

      The continue statement used anywhere within theloop-statement transfers control toiteration-expression.

      A program with an endless loop has undefined behavior if the loop has no observable behavior (I/O, volatile accesses, atomic or synchronization operation) in any part of itscond-expression,iteration-expression orloop-statement. This allows the compilers to optimize out all unobservable loops without proving that they terminate. The only exceptions are the loops wherecond-expression is omitted or is a constant expression;for(;;) is always an endless loop.

      As with all other selection and iteration statements, the for statement establishesblock scope: any identifier introduced in theinit-clause,cond-expression, oriteration-expression goes out of scope after theloop-statement.

      (since C99)

      attr-spec-seq is an optional list ofattributes, applied to thefor statement.

      (since C23)

      [edit]Keywords

      for

      [edit]Notes

      The expression statement used asloop-statement establishes its own block scope, distinct from the scope ofinit-clause, unlike in C++:

      for(int i=0;;){long i=1;// valid C, invalid C++// ...}

      It is possible to enter the body of a loop usinggoto. When entering a loop in this manner,init-clause andcond-expression are not executed. (If control then reaches the end of the loop body, repetition may occur including execution ofcond-expression.)

      [edit]Example

      Run this code
      #include <stdio.h>#include <stdlib.h>enum{ SIZE=8};int main(void){int array[SIZE];for(size_t i=0; i< SIZE;++i)        array[i]=rand()%2;printf("Array filled!\n");for(size_t i=0; i< SIZE;++i)printf("%d ", array[i]);putchar('\n');}

      Possible output:

      Array filled!1 0 1 1 1 1 0 0

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.8.5.3 The for statement (p: 110)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.8.5.3 The for statement (p: 151)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.8.5.3 The for statement (p: 136)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.6.5.3 The for statement

      [edit]See also

      C++ documentation forfor loop
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/for&oldid=135651"

      [8]ページ先頭

      ©2009-2025 Movatter.jp