Advertisement
//Labelled break example with while loop
public class LabelledBreak
{
public static void main(String... ar)
{
int i=7;
loop1:
while(i<20)
{
if(i==10)
break loop1;
System.out.println("i ="+i);
i++;
}
System.out.println("Out of the loop");
} //main method ends
} //class ends
7
8
9
Out of the loop
Advertisement
//Labelled break example with nested for loop
public class LabelledBreak
{
public static void main(String... ar)
{
loop2:
for(int i=0;i<2;i++)
for(int j=0;j<5;j++)
{
if(j==2)
break loop2;
System.out.println("i = "+i);
System.out.println("j = "+j);
}
System.out.println("Out of the loop");
} //main method ends
} //class ends
i = 0
j = 0
i = 0
j = 1
Out of the loop
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement