< Prev
Next >



Labelled Continue Statement





In Labelled Continue Statement, we give a label/name to a loop.

When this continue statement is encountered with the label/name of the loop, it skips the execution any statement within the loop for the current iteration and continues with the next iteration and condition checking in the labelled loop.






Labelled continue example


//Labelled continue example

class LabelledContinue
{
public static void main(String... ar)
{


loop:
for(int i=0;i<2;i++)
for(int j=0;j<5;j++)
{	
	if(j==2)
		continue loop;

	System.out.println("i ="+i);
	System.out.println("j ="+j);
}

System.out.println("Out of the loop");

} //main method ends

} //class ends



Output


i =0
j =0
i =0
j =1
i =1
j =0
i =1
j =1
Out of the loop

As you may see in the example, outer for-loop was named outer. When the labelled continue statement was encountered during the iteration, i equals to 0 and j equals to 2, it skipped executing the two print statements in the loop after it and the program continued with the next iteration i.e. i equals to 1 and j equals to 0.

When the labelled continue statement was encountered during the iteration, i equals to 1 and j equals to 2, it skipped executing the two print statements again and continued with the loop(which ended) and finally the print statement, System.out.println() is executed.



Note :


Label outer was mainly for the example and you could even give your own label/name to for loop/while loop in your program.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Labelled Break Statement
Arithmatic Operator>
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2