Continue Java Example

Sharing is healthy !!

In this post you are going to learn continue java example.

In java, continue is a statement. So this continue statement is normally used with the loops to skip the current iteration.

Continue statement skips the remaining part of the code at specified condition.

 

Let’s see an example where we get better understanding on continue statement,

EXAMPLE: CONTINUE JAVA EXAMPLE

Read Also – Java Constructor Example

public class JavaContinueExample
{
   public static void main(String[] args)
   {
      int continueArray[] = new int[]{2,4,6,8,10};
      System.out.println("Continue Example without 6 is: ");
      for(int x = 0;x < continueArray.length;x++)
      {
         if(continueArray[x] == 6)
         {
            continue;
         }
         System.out.println(continueArray[x]);
      }
   }
}

Output:

Continue Java Example


continue statement – for loop and while loop

public class ContinueDemo
{
   public static void main(String[] args)
   {
      System.out.println("Continue statement - using for loop - ");     
      for(int count1 = 1; count1 <= 10; count1++)
      {
         if(count1 == 6)
         {
            continue;
         }
         System.out.println(count1 + " Flower Brackets blog is best!!");
      }
      
      System.out.println("\n");
      System.out.println("Continue statement - using while loop - ");
      int count2 = 1;
      while(count2 <= 10)
      {
         if(count2 == 6)
         { 
            count2++;
            continue;
         }
         System.out.println(count2 + " Flower Brackets blog is best!!");
         count2++;
      }
   }
}

Output:

Continue Java Example

As you can see in the output (image above) now you guys see “count” six is not there in for loop and while loop.

The difference here is when you use the continue statement inside the for loop the control will be transferred to increment part of “for” loop.

Whilst, in “while” loop the control will be transferred to check the condition.


continue statement – with label and without label

The continue statement skips the current iteration of a loop (for, while and do-while).

// Continue statement without label

public class ContinueWithoutLabel
{
   public static void main(String[] args)
   {
      // outer for loop
      for(int a = 0; a < 2; a++)
      {
         System.out.println("a : " + a + "\n");
         // inner for loop
         for(int b = 0; b < 3; b++)
         {
            if(b == 1)
            {
               continue;
            }
            System.out.println("b : " + b);
         }
      }
   }
}

A labelled continue statement skips the current iteration of an outer loop marked with the given label.

// Continue statement with label

public class ContinueWithLabel
{
   public static void main(String[] args)
   {
      // outer for loop
      outer: for(int a = 0; a < 2; a++)
      {
         System.out.println("a : " + a + "\n");
         // inner for loop
         for(int b = 0; b < 3; b++)
         {
            if(b == 1)
            {
               // continue statement with label
               continue outer;
            }
            System.out.println("b : " + b);
         }
      }
   }
}

conclusion

That’s it guys. This is all about continue statement in java with an example java program.

I hope you have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Sharing is healthy !!