Kotlin Control Flow Statements
Estimated reading: 3 minutes 26 views

⛔ Kotlin – Break & Continue: Control Your Loops Effectively

🧲 Introduction – Why Learn Break & Continue in Kotlin?

When working with loops, sometimes you need to exit early or skip certain iterations. Kotlin provides two simple yet powerful keywords for this: break and continue. These control statements let you manage loop flow precisely—perfect for filtering, stopping loops based on conditions, or jumping to the next item.

🎯 In this guide, you’ll learn:

  • How break and continue work in Kotlin loops
  • Difference between unlabeled and labeled control flow
  • Real-world use cases with for, while, and nested loops
  • Best practices and common pitfalls

🔁 Kotlin break – Exit the Loop Immediately

The break statement terminates the current loop and proceeds with the next statement after the loop.

✅ Basic Example:

for (i in 1..10) {
    if (i == 5) break
    println(i)
}

🟢 Output:

1  
2  
3  
4

As soon as i equals 5, the loop breaks.


⏭️ Kotlin continue – Skip Current Iteration

The continue statement skips the current iteration and moves to the next one.

✅ Example:

for (i in 1..5) {
    if (i == 3) continue
    println(i)
}

🟢 Output:

1  
2  
4  
5

When i == 3, that iteration is skipped.


🔀 Using Break & Continue in While Loops

var i = 1

while (i <= 5) {
    if (i == 4) {
        i++
        continue
    }

    println("i = $i")

    if (i == 5) break
    i++
}

🟢 Output:

i = 1  
i = 2  
i = 3  
i = 5
  • Skips printing when i == 4
  • Breaks when i == 5

🔁 Labeled Break and Continue – For Nested Loops

Labeled loops help when breaking or continuing specific outer loops.

🎯 Labeled break:

outer@ for (i in 1..3) {
    for (j in 1..3) {
        if (i == 2 && j == 2) break@outer
        println("i = $i, j = $j")
    }
}

🟢 Output:

i = 1, j = 1  
i = 1, j = 2  
i = 1, j = 3  
i = 2, j = 1

✔️ break@outer exits both loops at once.


🎯 Labeled continue:

outer@ for (i in 1..3) {
    for (j in 1..3) {
        if (j == 2) continue@outer
        println("i = $i, j = $j")
    }
}

🟢 Output:

i = 1, j = 1  
i = 2, j = 1  
i = 3, j = 1

✔️ continue@outer jumps to the next iteration of the outer loop.


🚫 Common Mistakes

❌ Mistake✅ Fix
Forgetting to increment loop variableAlways update loop variable in while
Using break without clear exit planUse only when absolutely needed
Misusing unlabeled break in nested loopsUse labeled break@label for nested loops
Using continue when break is neededUnderstand difference: skip vs exit

✅ Best Practices for Break & Continue

PracticeReason
Use labeled control only when neededImproves readability and avoids confusion
Minimize break in large loopsImproves maintainability
Add comments when using control flowHelps others understand loop logic
Prefer when or filter for complex skipping logicMore declarative, Kotlin-idiomatic

📌 Summary – Recap & Next Steps

break and continue in Kotlin give you fine-grained control over loop execution. Use them to exit early or skip logic based on dynamic conditions. With labels, they become even more powerful—especially in nested loops.

🔍 Key Takeaways:

  • break exits the loop completely.
  • continue skips the current iteration and proceeds with the next.
  • Labeled versions allow targeting specific loops.
  • Avoid overusing control flow statements for cleaner logic.

⚙️ Practical Use:
Use these for search operations, data filtering, input validation, and nested loop control in Android apps, backend logic, or real-time games.


❓ FAQs – Kotlin Break & Continue

What is the difference between break and continue in Kotlin?
break exits the loop entirely, while continue skips the current iteration and proceeds to the next.


Can I break from nested loops in Kotlin?
✅ Yes. Use labeled break@label to exit outer loops.


Is using break or continue bad practice?
✅ No, but overusing them—especially in deeply nested loops—can reduce readability. Use labels only when necessary.


Can I use break and continue in for, while, and do-while?
✅ Yes. They are supported in all loop types in Kotlin.


What’s a real-world use of continue?
✅ Skipping invalid inputs, like this:

for (item in items) {
    if (item.isBlank()) continue
    println(item)
}

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Kotlin – Break & Continue

Or Copy Link

CONTENTS
Scroll to Top