title: Reverse iteration with an unsigned loop variable
body: <p>I've been discussing the use of size_t with colleagues.  One issue that has come up is loops that decrement the loop variable until it reaches zero.</p>
  
  <p>Consider the following code:</p>
  
  <pre><code>for (size_t i = n-1; i &gt;= 0; --i) { ... }
  </code></pre>
  
  <p>This causes an infinite loop due to unsigned integer wrap-around.  What do you do in this case?  It seems far to easy to write the above code and not realise that you've made a mistake.</p>
  
  <p>Two suggestions from our team are to use one of the following styles:</p>
  
  <pre><code>for (size_t i = n-1; i != -1 ; --i) { ... }
  
  for (size_t i = n; i-- &gt; 0 ; ) { ... }
  </code></pre>
  
  <p>But I do wonder what other options there are...</p>