Welcome to The Coding College! In this tutorial, we’ll dive deep into the break
and continue
statements in Kotlin. These control flow statements are essential for managing loops effectively and making your code more efficient.
What Are break
and continue
in Kotlin?
break
Statement: Used to terminate the nearest enclosing loop immediately.continue
Statement: Skips the current iteration of the loop and moves to the next one.
The break
Statement
The break
statement is used to exit a loop when a specific condition is met, skipping all remaining iterations.
Syntax:
break
Example: Exiting a Loop with break
fun main() {
for (i in 1..10) {
if (i == 5) {
println("Breaking the loop at i = $i")
break
}
println("i = $i")
}
}
Output:
i = 1
i = 2
i = 3
i = 4
Breaking the loop at i = 5
The continue
Statement
The continue
statement skips the current iteration and proceeds with the next iteration of the loop.
Syntax:
continue
Example: Skipping an Iteration with continue
fun main() {
for (i in 1..5) {
if (i == 3) {
println("Skipping i = $i")
continue
}
println("i = $i")
}
}
Output:
i = 1
i = 2
Skipping i = 3
i = 4
i = 5
Using break
and continue
in While Loops
These statements work seamlessly with while
and do...while
loops.
Example: break
in a While Loop
fun main() {
var num = 1
while (num <= 10) {
if (num == 6) {
println("Breaking the loop at num = $num")
break
}
println("num = $num")
num++
}
}
Output:
num = 1
num = 2
num = 3
num = 4
num = 5
Breaking the loop at num = 6
Example: continue
in a While Loop
fun main() {
var num = 0
while (num < 5) {
num++
if (num == 3) {
println("Skipping num = $num")
continue
}
println("num = $num")
}
}
Output:
num = 1
num = 2
Skipping num = 3
num = 4
num = 5
Labels with break
and continue
Kotlin allows you to use labels with break
and continue
to specify which loop to control, especially in nested loops.
Example: Labeled break
fun main() {
outer@ for (i in 1..3) {
for (j in 1..3) {
if (i == 2 && j == 2) {
println("Breaking outer loop at i=$i, j=$j")
break@outer
}
println("i=$i, j=$j")
}
}
}
Output:
i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
Breaking outer loop at i=2, j=2
Example: Labeled continue
fun main() {
outer@ for (i in 1..3) {
for (j in 1..3) {
if (i == 2 && j == 2) {
println("Skipping i=$i, j=$j")
continue@outer
}
println("i=$i, j=$j")
}
}
}
Output:
i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
Skipping i=2, j=2
i=3, j=1
i=3, j=2
i=3, j=3
Practical Uses of break
and continue
Example: Searching for a Value in a List
fun main() {
val numbers = listOf(10, 20, 30, 40, 50)
for (num in numbers) {
if (num == 30) {
println("Found $num. Breaking the loop.")
break
}
println("Checking $num")
}
}
Example: Skipping Specific Values
fun main() {
val numbers = 1..10
for (num in numbers) {
if (num % 2 == 0) {
continue
}
println("Odd number: $num")
}
}
Output:
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
Best Practices
- Use Labels Judiciously: Labels can make code harder to read if overused.
- Avoid Overusing
break
andcontinue
: Use them only when necessary to enhance code clarity. - Handle Infinite Loops Carefully: Ensure
break
is used appropriately in loops with potentially endless iterations.
Learn More on The Coding College
By understanding break
and continue
, you can write cleaner and more efficient Kotlin programs. Explore more Kotlin tutorials and programming concepts on The Coding College to master your coding skills.
Conclusion
The break
and continue
statements are powerful tools in Kotlin for controlling loops effectively. Whether you’re terminating a loop or skipping specific iterations, these statements provide flexibility and precision.