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
In what situation you need a nested loop?
How will you terminate outer loop from the block of the inner loop?
Write down the syntax of a nested for loop.
Give the output of the following Java program snippet based on nested loops:
int i,j; for (i=0; i<4; i++) { for (j=i; j>=0; j--) System.out.print(j); System.out.println(); }