title: Increment order in a while loop
body: <p>Here I have a simple algorithm that computes the square of a number by adding up all of the odd numbers equal to the inputted number, such that:</p>
  
  <pre><code>1 = 1 = 1-squared
  1 + 3 = 4 = 2-squared
  1 + 3 + 5 = 9 = 3-squared
  </code></pre>
  
  <p>and so forth. The algorithm is as follows:</p>
  
  <pre><code>int triangleSquare(int number) {
      int product = 0;
      int currentOdd = 1;
  
      while (number--) {
          product += currentOdd;
          currentOdd += 2;
      }
  
      return product;
  }
  </code></pre>
  
  <p>My question is about the segment: </p>
  
  <pre><code>while (number--) { ... }
  </code></pre>
  
  <p>For some reason I get different results between <code>while (--number)</code> and <code>while (number--)</code>, which I assume is because <code>--number</code> should decrement first before performing the while loop (and vice-versa for <code>number--</code>). My question is, why does it make a difference? Shouldn't the while loop always execute last, regardless of the increment order? I must be missing something obvious, but that seems very odd to me, almost as though <code>number--</code> makes the conditional behave like a <code>do {...} while()</code> loop.</p>