Labelled Loop

Labelled Loop : In java we can give a label to block of statements. To give a label to a loop, place it before the loop the loop with a colon(:) at the end.

Syntax : Label : for(.... ; .... ; .... )
                        {
                                 -----------------
                                 -----------------
                        }

class TestLabel
{
            public static void main(String args[])
            {
                        int i , j;
                        x: for(i = 0; i<2; i++)
                        {
                                    for(j=0; j<3; j++)
                                    {
                                                if(i == j)
                                                            continue x;
                                                System.out.println("Loop");
                                    }
                        }
            }
}

0 Comments:

Post a Comment