Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Continue statement in java

 
 

Share on Google+Share on Google+

Continue statement in java

Advertisement
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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2013

Related Tutorials

Discuss: Continue statement in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com