Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      for loop

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      for
      range-for(C++11)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
       

      Conditionally executes a statement repeatedly, where the statement does not need to manage the loop condition.

      Contents

      [edit]Syntax

      attr (optional)for (init-statementcondition (optional);expression (optional))statement
      attr -(since C++11) any number ofattributes
      init-statement - one of
      (since C++23)

      Note that anyinit-statement must end with a semicolon. This is why it is often described informally as an expression or a declaration followed by a semicolon.

      condition - acondition
      expression - anexpression (typically an expression that increments the loop counter)
      statement - astatement (typically a compound statement)

      [edit]Condition

      Acondition can either be anexpression or asimple declaration.

      • If it can be syntactically resolved as astructured binding declaration, it is interpreted as a structured binding declaration.
      (since C++26)
      • If it can be syntactically resolved as an expression, it is treated as an expression. Otherwise, it is treated as a declaration that is not a structured binding declaration(since C++26).

      When control reaches condition, the condition will yield a value, which is used to determine whetherstatement will be executed.

      [edit]Expression

      Ifcondition is an expression, the value it yields is the the value of the expression contextually converted tobool. If that conversion is ill-formed, the program is ill-formed.

      [edit]Declaration

      Ifcondition is a simple declaration, the value it yields is the value of the decision variable (see below) contextually converted tobool. If that conversion is ill-formed, the program is ill-formed.

      [edit]Non-structured binding declaration

      The declaration has the following restrictions:

      • Syntactically conforms to the following form:
      • type-specifier-seqdeclarator=assignment-expression
      (until C++11)
      • attribute-specifier-seq(optional)decl-specifier-seqdeclaratorbrace-or-equal-initializer
      (since C++11)

      The decision variable of the declaration is the declared variable.

      Structured binding declaration

      The declaration has the following restrictions:

      The decision variable of the declaration is the invented variableeintroduced by the declaration.

      (since C++26)

      [edit]Explanation

      Afor statement equivalent to:

      {
      init-statement
      while (condition)
      {
      statement
      expression;
      }

      }

      Except that

      • The scope ofinit-statement and the scope ofcondition are the same.
      • The scope ofstatement and the scope ofexpression are disjoint and nested within the scope ofinit-statement andcondition.
      • Executing acontinue statement instatement will evaluateexpression.
      • Emptycondition is equivalent totrue.

      If the loop needs to be terminated withinstatement, abreak statement can be used as terminating statement.

      If the current iteration needs to be terminated withinstatement, acontinue statement can be used as shortcut.

      [edit]Notes

      As is the case withwhile loop, ifstatement is not a compound statement, the scope of variables declared in it is limited to the loop body as if it was a compound statement.

      for(;;)int n;// n goes out of scope

      As part of the C++forward progress guarantee, the behavior isundefined if a loop that is not atrivial infinite loop(since C++26) withoutobservable behavior does not terminate. Compilers are permitted to remove such loops.

      While in C names declared in the scope ofinit-statement andcondition can be shadowed in the scope ofstatement, it is forbidden in C++:

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

      [edit]Keywords

      for

      [edit]Example

      Run this code
      #include <iostream>#include <vector> int main(){std::cout<<"1) typical loop with a single statement as the body:\n";for(int i=0; i<10;++i)std::cout<< i<<' '; std::cout<<"\n\n""2) init-statement can declare multiple names, as\n""long as they can use the same decl-specifier-seq:\n";for(int i=0,*p=&i; i<9; i+=2)std::cout<< i<<':'<<*p<<' '; std::cout<<"\n\n""3) condition may be a declaration:\n";char cstr[]="Hello";for(int n=0;char c= cstr[n];++n)std::cout<< c; std::cout<<"\n\n""4) init-statement can use the auto type specifier:\n";std::vector<int> v={3,1,4,1,5,9};for(auto iter= v.begin(); iter!= v.end();++iter)std::cout<<*iter<<' '; std::cout<<"\n\n""5) init-statement can be an expression:\n";int n=0;for(std::cout<<"Loop start\n";std::cout<<"Loop test\n";std::cout<<"Iteration "<<++n<<'\n'){if(n>1)break;} std::cout<<"\n""6) constructors and destructors of objects created\n""in the loop's body are called per each iteration:\n";struct S{        S(int x,int y){std::cout<<"S::S("<< x<<", "<< y<<"); ";}        ~S(){std::cout<<"S::~S()\n";}};for(int i{0}, j{5}; i< j;++i,--j)        S s{i, j}; std::cout<<"\n""7) init-statement can use structured bindings:\n";long arr[]{1,3,7};for(auto[i, j, k]= arr; i+ j< k;++i)std::cout<< i+ j<<' ';std::cout<<'\n';}

      Output:

      1) typical loop with a single statement as the body:0 1 2 3 4 5 6 7 8 9 2) init-statement can declare multiple names, aslong as they can use the same decl-specifier-seq:0:0 2:2 4:4 6:6 8:8 3) condition may be a declaration:Hello 4) init-statement can use the auto type specifier:3 1 4 1 5 9 5) init-statement can be an expression:Loop startLoop testIteration 1Loop testIteration 2Loop test 6) constructors and destructors of objects createdin the loop's body are called per each iteration:S::S(0, 5); S::~S()S::S(1, 4); S::~S()S::S(2, 3); S::~S() 7) init-statement can use structured bindings:4 5 6

      [edit]See also

      range-for loop(C++11) executes loop over range[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/for&oldid=173814"

      [8]ページ先頭

      ©2009-2025 Movatter.jp