Repeat while a condition remains true.

while-loop A while loop checks its condition before each pass. Keep loop limits small and clear.

While Loops

limit
WhileLoop.scala
Replay: real traced execution (multi-file project)
object Main {
  def main(args: Array[String]): Unit = {
    val limit = 3
    var count = 0
    var total = 0

    while (count < limit) {
      count = count + 1
      total = total + count
    }

    println("limit=" + limit)
    println("count=" + count)
    println("total=" + total)
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val limit = 1
    var count = 0
    var total = 0

    while (count < limit) {
      count = count + 1
      total = total + count
    }

    println("limit=" + limit)
    println("count=" + count)
    println("total=" + total)
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val limit = 5
    var count = 0
    var total = 0

    while (count < limit) {
      count = count + 1
      total = total + count
    }

    println("limit=" + limit)
    println("count=" + count)
    println("total=" + total)
  }
}
  1. limit ← 3, count ← 0, total ← 0

    1object Main {2  def main(args: Array[String]): Unit = {3    val limit→ 3 = 3 //@limit=1, 54    var count→ 0 = 05    var total→ 0 = 067    while (count < limit) {
  2. count ← 1, total ← 1

    pass 1 of 3
    7while (count0 < limit3) {8  count→ 1 = count + 19  total→ 1 = total + count110}
    All 3 passes — pass 1 is the card above
    passcounttotal
    10 10 1
    21 21 3
    32 33 6
  3. println("limit=" + limit)

    12    println("limit=" + limit3)13    println("count=" + count3)14    println("total=" + total6)15  }16}
    outputlimit=3
    count=3
    total=6
  1. limit ← 1, count ← 0, total ← 0

    1object Main {2  def main(args: Array[String]): Unit = {3    val limit→ 1 = 14    var count→ 0 = 05    var total→ 0 = 067    while (count < limit) {
  2. count ← 1, total ← 1

    7while (count0 < limit1) {8  count→ 1 = count + 19  total→ 1 = total + count110}
  3. println("limit=" + limit)

    12    println("limit=" + limit1)13    println("count=" + count1)14    println("total=" + total1)15  }16}
    outputlimit=1
    count=1
    total=1
  1. limit ← 5, count ← 0, total ← 0

    1object Main {2  def main(args: Array[String]): Unit = {3    val limit→ 5 = 54    var count→ 0 = 05    var total→ 0 = 067    while (count < limit) {
  2. count ← 1, total ← 1

    pass 1 of 5
    7while (count0 < limit5) {8  count→ 1 = count + 19  total→ 1 = total + count110}
    All 5 passes — pass 1 is the card above
    passcounttotal
    10 10 1
    21 21 3
    32 33 6
    43 46 10
    54 510 15
  3. println("limit=" + limit)

    12    println("limit=" + limit5)13    println("count=" + count5)14    println("total=" + total15)15  }16}
    outputlimit=5
    count=5
    total=15

Follow the Counter

  1. limit starts at 3.
  2. count and total both start at 0.
  3. Each pass adds 1 to count.
  4. The new count is added to total: 1, then 2, then 3.
  5. The program prints limit=3, count=3, and total=6. | limit | count path | total path | final total | | --- | --- | --- | --- | | 1 | 0 -> 1 | 0 -> 1 | 1 | | 3 | 0 -> 1 -> 2 -> 3 | 0 -> 1 -> 3 -> 6 | 6 | | 5 | 0 -> 1 -> 2 -> 3 -> 4 -> 5 | 0 -> 1 -> 3 -> 6 -> 10 -> 15 | 15 |

Exercise: WhileLoop.scala

Reproduce count=3 and total=6 for limit 3, then try limit 1 and limit 5 and predict the printed total.