title: New Swift for-in syntax for incrementing and decrementing loops
body: <p>TL;DR; Why doesn't the Swift range operator handle decreasing ranges?</p>
  
  <p>I have some code like this:</p>
  
  <pre><code>for var i = 0; i &lt; previousPage; i += 1 {
      purgePage(i)
  }
  </code></pre>
  
  <p>Which the swift compiler suggests I change to the <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0007-remove-c-style-for-loops.md" rel="nofollow">new Swift for loop</a> syntax:</p>
  
  <pre><code>C-style for statement is deprecated and will be removed in a future version of Swift
  Fix-it Replace "var i = 0; i &lt; previousPage; i += 1" with "i in 0 ..&lt; previousPage"
  </code></pre>
  
  <p>The resulting code now looks like this:</p>
  
  <pre><code>// Purge previous non-visible pages.
  for i in 0 ..&lt; previousPage {
      purgePage(i)
  }
  </code></pre>
  
  <p>The problem is that now the app crashes because <code>previousPage</code> can be a negative or a positive integer, forming either an increasing or decreasing range. When it's a decreasing range, I get the <code>EXC_BAD_INSTRUCTION</code>:</p>
  
  <pre><code>fatal error: Can't form Range with end &lt; start
  </code></pre>
  
  <p>How can I make the new syntax work in my case? Adding code to check the value of <code>previousPage</code> seems to be a step backwards from the old syntax.</p>