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

target
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)
  }
}
  1. 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) {
  2. checks ← 1

    pass 1 of 3
    8while (current1 <= 4 && !foundfalse) {9  checks→ 1 = checks + 110  if (current == target) {11    found = true
    All 3 passes — pass 1 is the card above
    passtargetcheckscurrentfound
    10 11 2false
    21 22 3false
    332 33true
  3. current ← 2

    pass 1 of 2
    11    found = true12  } else {13    current→ 2 = current + 114  }15}
  4. current ← 3

    pass 2 of 2
    11    found = true12  } else {13    current→ 3 = current + 114  }15}
  5. found ← true

    9checks = checks + 110if (current3 == target3) {11  found→ true = true12} else {13  current = current + 1
  6. status ← 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
  1. 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) {
  2. checks ← 1

    pass 1 of 4
    8while (current1 <= 4 && !foundfalse) {9  checks→ 1 = checks + 110  if (current == target) {11    found = true
    All 4 passes — pass 1 is the card above
    passcurrentchecks
    110 1
    221 2
    332 3
    443 4
  3. current ← 2

    pass 1 of 4
    11    found = true12  } else {13    current→ 2 = current + 114  }15}
    All 4 passes — pass 1 is the card above
    passcurrent
    11 2
    22 3
    33 4
    44 5
  4. 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

  1. target starts at 3.
  2. current starts at 1, checks starts at 0, and found starts as false.
  3. The loop checks 1, then 2, then 3.
  4. When current == target, found becomes true and the loop stops.
  5. The program prints target=3, checks=3, and status=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.