Continue statement in java

Ads
 

Continue statement in java

In this section we will discuss about continue statement in java. continue is one of the branching statement used in most of the programming languages like C,C++ and java etc.

In this section we will discuss about continue statement in java. continue is one of the branching statement used in most of the programming languages like C,C++ and java etc.

Continue statement in java

In this section we will discuss about continue statement in java. continue is one of the branching statement used in most of the programming languages like C,C++ and java etc. Sometime we need to skip some of the condition in the loop, we can do this with the use of continue statement.

Difference between break and continue is, break exit from the loop but continue will skip the statemen, which is written in the loop. continue statement  stop the normal flow of the loop and control return to the loop without executing  the statement written after the continue statement. Here is the example which shows the use of continue in the loop.

There are two form of continue statement:

  • Unlabeled continue statement.
  • Labeled continue statement.

Example : Unlabeled continue statement

 for(i=0;i<5;i++)
 {
 for(j=0;j<5;j++)
   {
  if(j==3) continue;
   System.out.println(" i "+i);
   }
  
}

In the above example when j become 3 inner for loop will skip the remaining code and control goes to the outer for loop.

Example : Labeled continue statement

outer :for(i=0;i<5;i++)
        {
         for(j=0j<5;j++)
         {
          if(j==3)continue outer;
          System.out.println(" i " +i);
       
          }
        }

When j become 3, inner for loop will jump to outer for loop which is labeled with the outer and next iteration of outer loop will be executed.

Example : A program using continue statement

public class ContinueStatement 
 {
   public static void main(String args[]) {
	  for(int i=0; i<5; i++)
	{
		for(int j=0;j<5;j++)
		{
			if(j==3)continue outer;
		System.out.println("i = " + i + " j = " + j);
		}
	   }
        }
    }

Output :  After compiling and executing the above program.

Ads

Ads