ABC of Programming
About Lesson

A labeled statement is a way to mark a statement with a label, which can then be used as a reference point for other statements. It’s primarily used in conjunction with break and continue statements to control the flow of execution in nested loops and switch statements.

The syntax of a labeled statement is as follows:

label: statement

Here’s what the syntax of a labeled statement means:

  • label: This is the label that you assign to the statement. It must be a valid identifier followed by a colon.
  • statement: This is any valid Java statement.

Here’s an example of a labeled statement in Java that uses a break statement to break out of a nested loop:

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i * j >= 30) {
break outerLoop;

In this example, we have a nested loop with an outer loop and an inner loop. The outer loop is labeled with the label outerLoop, and the inner loop is labeled with the label innerLoop. Inside the inner loop, we have an if statement that checks if the product of i and j is greater than or equal to 30. If it is, we use the break statement with the label outerLoop to break out of both loops.

The labeled statement is a powerful construct that can be used to control the flow of execution in complex control structures. However, it should be used sparingly, as overuse can make the code harder to read and maintain.

Join the conversation