Skip one value and stop at another.

loop `continue` skips to the next loop pass, and `break` exits the loop.

Break and Continue

stopAt
BreakContinue.kt
Replay: real traced execution (multi-file project)
fun main() {
    val stopAt = 5
    var total = 0

    for (number in 1..6) {
        if (number == 2) {
            continue
        }
        if (number == stopAt) {
            break
        }
        total += number
    }

    println("stopAt=$stopAt")
    println("total=$total")
}
fun main() {
    val stopAt = 3
    var total = 0

    for (number in 1..6) {
        if (number == 2) {
            continue
        }
        if (number == stopAt) {
            break
        }
        total += number
    }

    println("stopAt=$stopAt")
    println("total=$total")
}
fun main() {
    val stopAt = 6
    var total = 0

    for (number in 1..6) {
        if (number == 2) {
            continue
        }
        if (number == stopAt) {
            break
        }
        total += number
    }

    println("stopAt=$stopAt")
    println("total=$total")
}
  1. stopAt ← 5, total ← 0

    1fun main() {2    val stopAt→ 5 = 5 //@stopAt=3, 63    var total→ 0 = 0
  2. total ← 1

    pass 1 of 5
    5for (number1 in 1..6) {6    if (number == 2) {7        continue8    }9    if (number == stopAt) {10        break11    }12    total→ 1 += number113}
    All 5 passes — pass 1 is the card above
    passnumberstopAttotal
    110 1
    22
    331 4
    444 8
    555
  3. if (number == 2)

    5for (number in 1..6) {6    if (number2 == 2) {7        continue8    }
  4. if (number == stopAt)

    8}9if (number5 == stopAt5) {10    break11}
  5. println("stopAt=$stopAt")

    15    println("stopAt=$stopAt5")16    println("total=$total8")17}
    outputstopAt=5
    total=8
  1. stopAt ← 3, total ← 0

    1fun main() {2    val stopAt→ 3 = 33    var total→ 0 = 0
  2. total ← 1

    pass 1 of 3
    5for (number1 in 1..6) {6    if (number == 2) {7        continue8    }9    if (number == stopAt) {10        break11    }12    total→ 1 += number113}
    All 3 passes — pass 1 is the card above
    passnumberstopAttotal
    110 1
    22
    333
  3. if (number == 2)

    5for (number in 1..6) {6    if (number2 == 2) {7        continue8    }
  4. if (number == stopAt)

    8}9if (number3 == stopAt3) {10    break11}
  5. println("stopAt=$stopAt")

    15    println("stopAt=$stopAt3")16    println("total=$total1")17}
    outputstopAt=3
    total=1
  1. stopAt ← 6, total ← 0

    1fun main() {2    val stopAt→ 6 = 63    var total→ 0 = 0
  2. total ← 1

    pass 1 of 6
    5for (number1 in 1..6) {6    if (number == 2) {7        continue8    }9    if (number == stopAt) {10        break11    }12    total→ 1 += number113}
    All 6 passes — pass 1 is the card above
    passnumberstopAttotal
    110 1
    22
    331 4
    444 8
    558 13
    666
  3. if (number == 2)

    5for (number in 1..6) {6    if (number2 == 2) {7        continue8    }
  4. if (number == stopAt)

    8}9if (number6 == stopAt6) {10    break11}
  5. println("stopAt=$stopAt")

    15    println("stopAt=$stopAt6")16    println("total=$total13")17}
    outputstopAt=6
    total=13

Follow the Loop Control

  1. stopAt starts as 5.
  2. The loop checks numbers from 1 through 6.
  3. continue skips 2, so it is not added.
  4. break stops when the number reaches 5, before adding it.
  5. The total is 1 + 3 + 4 = 8, so the output is total=8. | number | action | total after | | --- | --- | --- | | 1 | add | 1 | | 2 | skip | 1 | | 3 | add | 4 | | 4 | add | 8 | | 5 | break | 8 |

Exercise: BreakContinue.kt

Reproduce stopAt=5 and total=8, then use the pinned stopAt variants 3 and 6 to predict totals 1 and 13.