title: Scope of declarations in the body of a do-while statement
body: <p>In <a href="https://stackoverflow.com/q/27430640/1708801">Why can't you declare a variable inside a do while loop?</a> the OP asks why a declaration in the while-condition of a do-while loop isn't in scope in the do-statement. That would be very unnatural as C/C++ generally follow a "declaration-at-top-of-scope" pattern. But what about the converse - why not extend the scope of any declaration in the do-statement to the while-condition. That would allow </p>
  
  <pre><code>int i;
  do {
    i = get_data();
    // whatever you want to do with i;
  } while (i != 0);
  </code></pre>
  
  <p>to be shortened to</p>
  
  <pre><code>do {
    int i = get_data();
    // whatever you want to do with i;
  } while (i != 0);
  </code></pre>
  
  <p>which gives a tidy syntax for limiting the scope of the control variable. (Note: I'm aware that this isn't valid syntax - that's the point of the question, why not extend the language to allow this syntax.) </p>
  
  <p>As I note in a comment below, this extension would not break existing code, and would be very much in then spirit of the introduction of for-init (and while-init) scoping. </p>