Why is for/while loop variable incrementing by 1 after loop exit? Unwanted side effect
<p>Here is the code -</p>

<pre><code>int i = 0;

System.out.printf( "\n%d\n\n", i );  // show variable i before loop

for( i = 0; i &lt; 8; i++ )
{
    System.out.printf( "%d\t", i );
}

System.out.printf( "\n\n%d\n", i );   // show variable i after loop
</code></pre>

<p>Here is the output -</p>

<blockquote>
  <p>0</p>
  
  <p>0  1   2   3   4   5   6   7   </p>
  
  <p>8</p>
</blockquote>

<p>My problem arises when I want to use the variable i after the exit of the for loop.  I would assume that i is reading 7, the 8th increment in zero based counting but it actually reads as 8!!!  One more increment to variable i has been made on loop exit.</p>

<p>In order to remedy this I would have to do something like i--  at the end of the loop and before using it in any further code.  This seems to me to make the code harder to understand.</p>

<p>Is there a better solution?</p>
