Kotlin Break and Continue

Kotlin Tutorial

Kotlin break and continue – Complete Beginner Guide With Examples

Control flow is a core concept in programming. When working with loops in Kotlin, you often need to either stop a loop early or skip certain iterations. That’s where break and continue come into play.

These two keywords help you control how loops behave, making your code cleaner and more efficient.

In this SEO-optimized, beginner-friendly guide, you’ll learn:

  • What break and continue mean

  • How they work in Kotlin loops

  • Using break in for, while, and do-while loops

  • Using continue properly

  • Labeled break and continue

  • Real-world examples

  • Common beginner mistakes

  • Best practices

Let’s get started


Understanding Loops in Kotlin

Before learning break and continue, you must understand loops.

Kotlin provides:

  • for loop

  • while loop

  • do-while loop

Loops repeat a block of code until a condition becomes false.

Example:

Output:

1
2
3
4
5

Now let’s see how break and continue change loop behavior.


What Is break in Kotlin?

The break keyword immediately stops a loop.

When Kotlin encounters break, it exits the loop completely.


Basic Example of break

Output:

1
2
3
4

As soon as i becomes 5, the loop stops.


Using break in a while Loop


 

Output:

1
2
3
4
5

The loop exits when num reaches 6.


What Is continue in Kotlin?

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

Unlike break, it does not stop the loop.


Basic Example of continue

Output:

1
2
4
5

When i equals 3, that iteration is skipped.


Difference Between break and continue

Featurebreakcontinue
Stops loop completelyYesNo
Skips only one iterationNoYes
Used in loopsYesYes
Useful for early exitYesNo

Use break when you want to stop the loop entirely.
Use continue when you want to skip certain values.


Real-World Example – Searching in List

Suppose you want to find a number in a list.


 

Once 30 is found, the loop stops. This improves performance.


Real-World Example – Skipping Invalid Data


 

Output:

50
70
90

Negative scores are ignored.


Labeled break in Kotlin

In nested loops, break only exits the inner loop.

If you want to exit the outer loop, use labels.


Example Without Label

Only the inner loop stops.


Example With Label

Now both loops stop.


Labeled continue in Kotlin

You can also use labels with continue.

Example:

This skips to the next iteration of the outer loop.


Using break in do-while Loop


 

Output:

1
2
3

Best Practices for Using break and continue

  •  Keep loop logic simple
  •  Avoid excessive nesting
  •  Use labels carefully
  •  Prefer readable conditions
  •  Do not overuse continue

Readable code is always better than clever code.


Common Beginner Mistakes

 Using break Outside Loop

break only works inside loops.


 Infinite Loops

Forgetting to update loop variable:

Use break to avoid infinite loops.


 Overusing Labels

Labels can make code harder to read.


When Should You Use break?

Use break when:

  • Searching for a value

  • Validating input

  • Stopping loop early

  • Avoiding unnecessary iterations


When Should You Use continue?

Use continue when:

  • Skipping invalid data

  • Ignoring unwanted values

  • Filtering inside loop


break and continue vs return

return exits the entire function.

Example:

Output:

1
2

Use return when you want to exit function completely.


Performance Considerations

Using break improves performance by:

  • Reducing unnecessary iterations

  • Stopping search early

  • Preventing heavy computations

However, overusing continue can reduce readability.


Frequently Asked Questions (FAQs)

1. What is break in Kotlin?

break stops the loop immediately.

2. What is continue in Kotlin?

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

3. Can break be used outside loops?

No, break can only be used inside loops.

4. What are labeled breaks?

Labeled breaks allow exiting outer loops in nested structures.

5. Which is better, break or continue?

It depends on your logic. Use break to stop loop entirely and continue to skip specific iterations.


Conclusion

Kotlin break and continue are essential control flow tools.

You learned:

  • How break works

  • How continue works

  • Difference between them

  • Labeled break and continue

  • Real-world use cases

  • Common mistakes

Mastering these keywords will help you write cleaner and more efficient Kotlin loops.

You may also like...