Control Flow
Break and Continue
Skip one value and stop at another.
Break and Continue
BreakContinue.kt
fun main() {
val stopAt =
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 =
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 =
var total = 0
for (number in 1..6) {
if (number == 2) {
continue
}
if (number == stopAt) {
break
}
total += number
}
println("stopAt=$stopAt")
println("total=$total")
}
loop
`continue` skips to the next loop pass, and `break` exits the loop.