Why can't I put a variable declaration in the test portion of a while loop?
<p>You can, obviously, put a variable declaration in a for loop:</p>

<pre><code>for (int i = 0; ...
</code></pre>

<p>and I've noticed that you can do the same thing in if and switch statements as well:</p>

<pre><code>if ((int i = f()) != 0) ...

switch (int ch = stream.get()) ...
</code></pre>

<p>But when I try to do the same thing in a while loop:</p>

<pre><code>while ((int ch = stream.get()) != -1) ...
</code></pre>

<p>The compiler (VC++ 9.0) does not like it at all.</p>

<p>Is this compliant behavior? Is there a reason for it?</p>

<p><strong>EDIT</strong>: I found I can do this:</p>

<pre><code>while (int ch = stream.get() != -1) ...
</code></pre>

<p>but because of precedence rules, that's interpreted as:</p>

<pre><code>while (int ch = (stream.get() != -1)) ...
</code></pre>

<p>which is not what I want.</p>
