title: Why are Scala for loops slower than logically identical while loops?
body: <p>I love the power of Scala's for comprehensions and the way they integrate with any monadic type with map and flatMap. However, I'd also like to do simple integer loops without a major speed penalty. Why doesn't Scala have the following two logically identical loops run with similar runtime performance or even compile into similar byte code?</p>
  
  <pre><code>// This is slow...
  for (i &lt;- 0 until n) println(s"for loop with $i")
  
  // This runs much faster. It runs roughly at the same speed as Java code doing an identical while or for loop.
  var i = 0;
  while (i &lt; n) {
    println(s"while loop with $i")
    i += 1
  }
  </code></pre>