Control Flow
While Loops
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
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)
}
}
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) {count ← 1, total ← 1
pass 1 of 37while (count0 < limit3) {8 count→ 1 = count + 19 total→ 1 = total + count110}All 3 passes — pass 1 is the card above pass counttotal1 0 → 1 0 → 1 2 1 → 2 1 → 3 3 2 → 3 3 → 6 println("limit=" + limit)
12 println("limit=" + limit3)13 println("count=" + count3)14 println("total=" + total6)15 }16}outputlimit=3 count=3 total=6
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) {count ← 1, total ← 1
7while (count0 < limit1) {8 count→ 1 = count + 19 total→ 1 = total + count110}println("limit=" + limit)
12 println("limit=" + limit1)13 println("count=" + count1)14 println("total=" + total1)15 }16}outputlimit=1 count=1 total=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) {count ← 1, total ← 1
pass 1 of 57while (count0 < limit5) {8 count→ 1 = count + 19 total→ 1 = total + count110}All 5 passes — pass 1 is the card above pass counttotal1 0 → 1 0 → 1 2 1 → 2 1 → 3 3 2 → 3 3 → 6 4 3 → 4 6 → 10 5 4 → 5 10 → 15 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
limitstarts at3.countandtotalboth start at0.- Each pass adds
1tocount. - The new
countis added tototal:1, then2, then3. - The program prints
limit=3,count=3, andtotal=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.