title: C++ for variable scope
body: <p>I'm new to c++ and came across this:</p>
  
  <pre><code>    for (int i=0 ; i&lt;500 ; i++) {
          //to do
      }
  
      int i;
      std::cin &gt;&gt; i;
  </code></pre>
  
  <p>UPDATE: Using visual studio 2010 (and the compiler it uses) the i outside the for loop has the value 500 AFTER the call to cin >> i; I am looking at the values using the Visual Studio debugger &lt;&lt; (this is the problem - thanks Greg)</p>
  
  <p>Now I expected </p>
  
  <p>a) the int i; outside the for loop and the int i=0; to be different variables i.e. the for loop i to go out of scope once the braces close</p>
  
  <p>b) once I realized that it wasn't going out of scope I wondered why it was not getting over-written by the cin.</p>
  
  <p>I think b) is intentional (though I'm having trouble finding the page its on in Bjarne Stroustrup's book on c++) but I am sure it states in the same book that variables have scope within the braces they are enclosed in.</p>
  
  <p>In most other languages I know when declaring variables in the for construct they are considered to be in the braces of the for loop but in c++ this doesn't seem to be the case.</p>
  
  <p>Is this the case and is this specific to the for loop or are there any other cases where this happens (I can't think of any other but hey I'm new)</p>
  
  <p>Thanks</p>