Control Flow
Flag Loops
Use a boolean flag to stop a loop early.
flag-loop
A boolean flag can record when a search is finished. The loop condition can then stop without using a special break statement.
Flag Loops
FlagLoop.scala
Replay: real traced execution (multi-file project)
object Main {
def main(args: Array[String]): Unit = {
val target = 3
var current = 1
var checks = 0
var found = false
while (current <= 4 && !found) {
checks = checks + 1
if (current == target) {
found = true
} else {
current = current + 1
}
}
val status = if (found) "found" else "missing"
println("target=" + target)
println("checks=" + checks)
println("status=" + status)
}
}
object Main {
def main(args: Array[String]): Unit = {
val target = 5
var current = 1
var checks = 0
var found = false
while (current <= 4 && !found) {
checks = checks + 1
if (current == target) {
found = true
} else {
current = current + 1
}
}
val status = if (found) "found" else "missing"
println("target=" + target)
println("checks=" + checks)
println("status=" + status)
}
}
target ← 3, current ← 1, checks ← 0, found ← false
1object Main {2 def main(args: Array[String]): Unit = {3 val target→ 3 = 3 //@target=54 var current→ 1 = 15 var checks→ 0 = 06 var found→ false = false78 while (current <= 4 && !found) {checks ← 1
pass 1 of 38while (current1 <= 4 && !foundfalse) {9 checks→ 1 = checks + 110 if (current == target) {11 found = trueAll 3 passes — pass 1 is the card above pass targetcheckscurrentfound1 — 0 → 1 1 → 2 false 2 — 1 → 2 2 → 3 false 3 3 2 → 3 3 true current ← 2
pass 1 of 211 found = true12 } else {13 current→ 2 = current + 114 }15}current ← 3
pass 2 of 211 found = true12 } else {13 current→ 3 = current + 114 }15}found ← true
9checks = checks + 110if (current3 == target3) {11 found→ true = true12} else {13 current = current + 1status ← found
17 val status→ found = if (foundtrue) "found" else "missing"1819 println("target=" + target3)20 println("checks=" + checks3)21 println("status=" + statusfound)22 }23}outputtarget=3 checks=3 status=found
target ← 5, current ← 1, checks ← 0, found ← false
1object Main {2 def main(args: Array[String]): Unit = {3 val target→ 5 = 54 var current→ 1 = 15 var checks→ 0 = 06 var found→ false = false78 while (current <= 4 && !found) {checks ← 1
pass 1 of 48while (current1 <= 4 && !foundfalse) {9 checks→ 1 = checks + 110 if (current == target) {11 found = trueAll 4 passes — pass 1 is the card above pass currentchecks1 1 0 → 1 2 2 1 → 2 3 3 2 → 3 4 4 3 → 4 current ← 2
pass 1 of 411 found = true12 } else {13 current→ 2 = current + 114 }15}All 4 passes — pass 1 is the card above pass current1 1 → 2 2 2 → 3 3 3 → 4 4 4 → 5 status ← missing
17 val status→ missing = if (foundfalse) "found" else "missing"1819 println("target=" + target5)20 println("checks=" + checks4)21 println("status=" + statusmissing)22 }23}outputtarget=5 checks=4 status=missing
Follow the Flag
targetstarts at3.currentstarts at1,checksstarts at0, andfoundstarts asfalse.- The loop checks
1, then2, then3. - When
current == target,foundbecomestrueand the loop stops. - The program prints
target=3,checks=3, andstatus=found. | target | checked values | checks | status | | --- | --- | --- | --- | | 3 | 1, 2, 3 | 3 | found | | 5 | 1, 2, 3, 4 | 4 | missing |
Exercise: FlagLoop.scala
Reproduce status=found for target 3, then try target 5 and predict why the status becomes missing.