title: Variables in scope during for loop iteration statement in C++
body: <p>According to my understanding of the C++ specification (according to the draft standard online), a for-loop can be rewritten in terms of a while-loop and a block for initialization.  According to my understanding, the iteration statement of the for-loop occurs in the same scope as the body, so it should be able to use variables declared in the body of the for-loop.  Both gcc and clang reject the following (contrived) code, which is a simplification of my real code.</p>
  
  <p>I can obviously fix the code by declaring j outside of the loop, but why is j out of scope below?</p>
  
  <pre><code>int main() 
  {
      for(int i=0; i&lt;10; i=j) int j=i+1;
  
      // // I must misunderstand the standard because I thought the above loop is 
      // // equivalent to the commented code below where j is clearly in scope.
      // {
      //     int i=0;
      //     while(i&lt;10) {
      //         int j=i+1;
      //         i=j;
      //     }
      // }
  
      return 0;
   }
  </code></pre>
  
  <p>According to clang (and gcc), this is invalid.</p>
  
  <pre><code>test.cpp:3:26: error: use of undeclared identifier 'j'
      for(int i=0; i&lt;10; i=j) int j=i+1;
                           ^
  1 error generated.
  </code></pre>