Introduction
The break
statement in Kotlin is used to terminate the nearest enclosing loop immediately, regardless of the loop condition. This is useful when you want to exit a loop based on a specific condition before the loop would naturally terminate. This chapter will cover the syntax and usage of the break
statement in Kotlin with examples.
Basic break Statement
The break
statement can be used inside any loop (for
, while
, or do-while
). When the break
statement is executed, the loop is terminated, and control is transferred to the statement immediately following the loop.
Syntax
break
Example with for
Loop
fun main() {
for (i in 1..10) {
if (i == 5) {
break
}
println("Iteration $i")
}
println("Loop terminated")
}
Explanation:
- The loop iterates from 1 to 10.
- When
i
equals 5, thebreak
statement is executed, and the loop terminates. - The output will be:
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Loop terminated
Example with while
Loop
fun main() {
var i = 1
while (i <= 10) {
if (i == 5) {
break
}
println("Iteration $i")
i++
}
println("Loop terminated")
}
Explanation:
- The loop iterates while
i
is less than or equal to 10. - When
i
equals 5, thebreak
statement is executed, and the loop terminates. - The output will be:
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Loop terminated
Example with do-while
Loop
fun main() {
var i = 1
do {
if (i == 5) {
break
}
println("Iteration $i")
i++
} while (i <= 10)
println("Loop terminated")
}
Explanation:
- The loop iterates while
i
is less than or equal to 10. - When
i
equals 5, thebreak
statement is executed, and the loop terminates. - The output will be:
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Loop terminated
Nested Loops
When using nested loops, the break
statement only terminates the nearest enclosing loop. To break out of multiple nested loops, you can use labels.
Example with Nested Loops
fun main() {
for (i in 1..3) {
for (j in 1..3) {
if (i == 2 && j == 2) {
break
}
println("i = $i, j = $j")
}
}
println("Loop terminated")
}
Explanation:
- The outer loop iterates from 1 to 3.
- The inner loop iterates from 1 to 3 for each iteration of the outer loop.
- When
i
equals 2 andj
equals 2, thebreak
statement is executed, and only the inner loop terminates. - The output will be:
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 Loop terminated
Labeled break Statement
To break out of multiple nested loops, you can use labeled break
statements. A label is defined by placing an identifier followed by @
before the loop.
Syntax
label@ for (item in collection) {
for (subItem in subCollection) {
if (condition) {
break@label
}
}
}
Example with Labeled break
fun main() {
outer@ for (i in 1..3) {
for (j in 1..3) {
if (i == 2 && j == 2) {
break@outer
}
println("i = $i, j = $j")
}
}
println("Loop terminated")
}
Explanation:
outer@
is a label for the outer loop.- When
i
equals 2 andj
equals 2, thebreak@outer
statement is executed, and both the inner and outer loops terminate. - The output will be:
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 Loop terminated
Conclusion
In this chapter, you learned about the break
statement in Kotlin, including its syntax and usage to terminate loops prematurely. You also saw examples of using the break
statement in for
, while
, and do-while
loops, as well as handling nested loops with labeled break
statements. Understanding how to use the break
statement is essential for controlling the flow of your Kotlin programs and managing loops effectively.