title: How to increment a counter for a while loop within the loop?
body: <p>I have a feeling I'm gonna feel really stupid here, but I'm just learning about using <code>++</code> and <code>--</code> to increment and decrements variables for while loops, and was wondering why this piece of code works and why this doesn't?</p>
  
  <p>Bad code:</p>
  
  <pre><code>int ctr = 0;
  while (ctr &lt; 10)
    printf("%d",ctr);
    ctr=ctr+1;
  </code></pre>
  
  <p>The bad code outputs zeros indefinitely.</p>
  
  <p>Working code:</p>
  
  <pre><code>int ctr=0;
  while (ctr++ &lt; 10)
      printf("%d",ctr);
  </code></pre>
  
  <p>The idea is for the output to be 012345678910 but even in the working code, it starts at 1 and goes to 10, instead of starting at 0. Even though the initial value of ctr is 0.</p>