Break and continue keywords work like they do in other languages.
while(true) {
if(condition1) {
continue // Will immediately start the next iteration, without executing the rest of the loop body
}
if(condition2) {
break // Will exit the loop completely
}
}
If you have nested loops, you can label the loop statements and qualify the break and continue statements to specify which loop you want to continue or break:
outer@ for(i in 0..10) {
inner@ for(j in 0..10) {
break // Will break the inner loop
break@inner // Will break the inner loop
break@outer // Will break the outer loop
}
}
This approach won't work for the functional forEach
construct, though.