KnowledgeBoat Logo

Computer Applications

What do you mean by labelled break statement? Give an example.

Java Nested for Loops

39 Likes

Answer

Labelled break statement transfers program control out of the code block whose label is specified as its target. The target code block must enclose the break statement but it does not need to be the immediately enclosing block.

In the below code snippet:

first: for (int j = 1; j <= 5; j++) {
    for (int k = 1; k <= j; k++) {
        if (k > 4)
            break first;
        System.out.print(k);
    }
    System.out.println();
}
System.out.println("Outside code block labelled first");

the labelled break statement break first; will transfer the program control outside the outer for loop to the statement System.out.println("Outside code block labelled first");

Answered By

23 Likes


Related Questions