Write about Labeled loops in Java

Labeled loops:

In java we can give a label to a block of statements a label is any valid java variable name. to give a label to a loop, place it before the loop with a colon(:) at the end.

For eg:

loop1: for(……..)
{
…………..
…………..
}

A block of statements can be labeled as shown below.

Block2:
{
…………..
…………...
Block 2:
{
…………..
…………..
}
………….
…………..
}

We have seen that simple break statement causes the control to jump outside the nearest loop and a simple continue statement restarts the current loop. If we want to jump outside a nested loops or to continue a loop. i. e outside the current one then we may have to use the labeled break and labelled continue statements.

Eg:

 outer: for(int m=1; m < 10; m++)
{
for(int n=1; n < 10;n++)
{
System.out.println(“ “+m*n);
if(m==n)
continue outer;
}
}


Here they continue statement terminates the inner loop when m==n and continuous with the next iteration of the outer loop.

Related

Java 3696951667485057408

Post a Comment

emo-but-icon

item