Control Flow
Break and Continue
Skip one value and stop at another.
loop
`continue` skips to the next loop pass, and `break` exits the loop.
Break and Continue
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")
}
stopAt ← 5, total ← 0
1fun main() {2 val stopAt→ 5 = 5 //@stopAt=3, 63 var total→ 0 = 0total ← 1
pass 1 of 55for (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 pass numberstopAttotal1 1 — 0 → 1 2 2 — — 3 3 — 1 → 4 4 4 — 4 → 8 5 5 5 — if (number == 2)
5for (number in 1..6) {6 if (number2 == 2) {7 continue8 }if (number == stopAt)
8}9if (number5 == stopAt5) {10 break11}println("stopAt=$stopAt")
15 println("stopAt=$stopAt5")16 println("total=$total8")17}outputstopAt=5 total=8
stopAt ← 3, total ← 0
1fun main() {2 val stopAt→ 3 = 33 var total→ 0 = 0total ← 1
pass 1 of 35for (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 pass numberstopAttotal1 1 — 0 → 1 2 2 — — 3 3 3 — if (number == 2)
5for (number in 1..6) {6 if (number2 == 2) {7 continue8 }if (number == stopAt)
8}9if (number3 == stopAt3) {10 break11}println("stopAt=$stopAt")
15 println("stopAt=$stopAt3")16 println("total=$total1")17}outputstopAt=3 total=1
stopAt ← 6, total ← 0
1fun main() {2 val stopAt→ 6 = 63 var total→ 0 = 0total ← 1
pass 1 of 65for (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 pass numberstopAttotal1 1 — 0 → 1 2 2 — — 3 3 — 1 → 4 4 4 — 4 → 8 5 5 — 8 → 13 6 6 6 — if (number == 2)
5for (number in 1..6) {6 if (number2 == 2) {7 continue8 }if (number == stopAt)
8}9if (number6 == stopAt6) {10 break11}println("stopAt=$stopAt")
15 println("stopAt=$stopAt6")16 println("total=$total13")17}outputstopAt=6 total=13
Follow the Loop Control
stopAtstarts as5.- The loop checks numbers from
1through6. continueskips2, so it is not added.breakstops when the number reaches5, before adding it.- The total is
1 + 3 + 4 = 8, so the output istotal=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.