Break and Continue in Kotlin

Learn via video courses
Topics Covered

Overview

Break and continue in kotlin are control flow statements used within loops to modify the normal iteration behavior. With break, you can abruptly exit a loop, terminating its execution prematurely, usually based on a specific condition. This is handy when you need to end the loop as soon as a condition is fulfilled. On the other hand, the continue statement, allows you to skip the remaining code within the current iteration of a loop and proceed to the next iteration.

Introduction

Break and continue in Kotlin are two special commands that help you control how loops work. These statements offer the ability to alter the standard iteration behavior, allowing programmers to fine-tune how loops execute based on specific conditions.

Imagine a list search: The break command lets you stop looking immediately and move on to something else outside of the list. This can save time because you don't have to keep looking through things you don't need. Similarly, The continue command allows you to just skip those things and move to the next one on the list. It's like saying, "I'll skip this one and go to the next, please".

Note: break and continue is useful, however excessive use of such control flow statements can make the code less maintainable and readable.

Kotlin break

Unlabeled Break

The break statement without a label is used to abruptly exit the innermost loop it's contained within. It's often used when a specific condition is met, and you want to stop the loop immediately. Here's how it works:

In this example, we have a loop that goes from 1 to 5. Inside the loop, we print the current iteration number. When the iteration number becomes 3, we use the break statement to stop the loop. The output will be:

Output:

Labeled Break

Sometimes, you might have nested loops, and you want to break out of a specific outer loop from within an inner loop. This is where labeled breaks come in handy. You can give a loop a label (a name), and then use that label with the break statement to exit that particular loop. Here's an example:

In this code, we have an outer loop labeled as outerLoop and an inner loop. When the values of i and j are both 2, we use the break@outerLoop statement to exit the outer loop. The output will be:

Output:

Notice how the outer loop stops immediately once the condition in the inner loop is met, thanks to the labeled break.

Kotlin continue

Unlabeled continue

The continue statement without a label is used to skip the rest of the current iteration of the innermost loop it's within and move on to the next iteration. It's particularly helpful when you want to skip specific steps of the loop's execution based on a certain condition. Here's an example to illustrate this:

In this code, when the value of i is 3, the continue statement is encountered, and the loop skips the remaining code for that iteration, moving directly to the next iteration. The output will be:

Output:

In the above output:

  • The for loop runs from 1 to 5 (inclusive).
  • Inside the loop, the code checks whether the value of i is equal to 3.
  • For i = 1, 2, 4 and 5, the condition is not met, so it prints Iteration 1, Iteration 2 and so on.
  • For i = 3, the condition is met, so it prints Skipping iteration 3 and uses the continue statement to skip the rest of the loop body and jump to the next iteration.

Labeled Continue:

Similar to the labeled break, you can also use labeled "continue" to control the flow of nested loops. This allows you to skip to the next iteration of a specific outer loop from within an inner loop. Here's an example:

In this code, when both i and j are 2, the continue@outerLoop statement is used to skip the current iteration of the outer loop and move to the next i value. The output will be:

Output:

The outer loop goes from 1 to 3, and the inner loop also goes from 1 to 3. When both i and j are 2, the code prints Skipping i: 2, j: 2 and continues to the next iteration of the outer loop. For all other combinations of i and j, it prints their values. This results in skipping the print for i: 2, j: 2 and printing the rest of the combinations.

Conclusion

  • Kotlin, a modern programming language, incorporates break and continue statements to manage loop behavior effectively.
  • The break statement is utilized to exit loops prematurely, optimizing program execution by stopping unnecessary iterations.
  • Conversely, the continue statement skips current loop iterations, enhancing efficiency by bypassing specific steps.
  • Labeled break and continue in kotlin statements offer control within nested loops, enabling precise manipulation of multiple loop levels.
  • By using these statements judiciously, developers can streamline code and enhance its readability.
  • Care should be taken to strike a balance between optimizing loop performance and maintaining code clarity.