title: How does a for loop check its conditions in Java?
body: <p>My question has to do with the order in which java checks the conditions of a for loop when there is a print statement <strong>in</strong> the "conditions" of the loop. It seems like an unpractical thing to do (I haven't ever seen it used in any practical way), though my lack of understanding of what is printed has me thinking that I may not fully understand how a for loop functions. The following question showed up on a recent exam:</p>
  
  <p>What will the following method print with an input of n = 5?</p>
  
  <pre><code>public static void mystery(int n) {
      for (int i = -1; i &lt; n; System.out.print(i + " ")) {
           i++;
      }
  }
  </code></pre>
  
  <p>The correct answer is:
  0 1 2 3 4 5</p>
  
  <p>To me, it seems that the loop ought to print -1, then increment i by 1, print 0 ..... until i = 4. Then it would print 4, increment i by 1, and break out of the loop at the loop's condition i &lt; n.
  Why is the correct answer what it is and why is my logic flawed?</p>