Continue Java Example

Welcome to flower brackets blog. In this post you are going to learn continue java example.

continue java example

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

What does continue do in java?

Java continue statement skips the remaining part of the code at specified condition.

Also Read – Java Constructor Example

Flow Diagram :

Continue Java Example

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

Example : continue java 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

Now let’s see for loop continue java and java continue while loop with an example,

public class ContinueDemo
{
   public static void main(String[] args)
   {
      System.out.println("Continue statement - for loop continue java - ");     
      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 - java continue 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 java 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 java 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);
         }
      }
   }
}


Continue statement using do while loop

Let’s see what does continue do in java using do while loop. Here’s example,

public class ContinueStatement
{
   public static void main(String[] args)
   {
      int a = 0;
      System.out.println("Continue statement java");
      do
      {
         if(a == 2)
         {
            a++;
            continue;
         }
         System.out.print(a + " ");
         a++;
      }while(a < 10);
   }
}

Output:

Continue statement java
0 1 3 4 5 6 7 8 9


conclusion

That’s it guys. This is all about how to use continue in java.

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

Do share this article if you like.

You May Also Like