Labelled Loops

A labelled loop in Java allows you to specify which loop to break or continue when dealing with nested loops. This makes it easier to control the flow of execution in complex scenarios.

Syntax

Here, label_name is the label given to the loop, and you use break or continue with the label to control flow.

  • Labelled loops help manage the flow of control in nested loops.
  • break label_name; exits the loop with the specified label.
  • continue label_name; skips the remaining code in the loop with the specified label and moves to the next iteration.

Examples

  • Using a labelled break

Output

  • Using a labelled continue

Output