Continue statement in java

In this post you are going to learn continue statement in java.

Continue statement in java is used in many loop control structures. Continue keyword will skip a particular iteration and move to next iteration.

Also read – constructor in java

Continue keyword can be used with the loops, namely, for loop, do loop and do while loop.

continue statement in java

Syntax

continue;


Java program of continue in “for” loop

In “for” loop if continue keyword is encountered, the control will move to the update statement. Let’s see java program,

public class ContinueExample 
{
   public static void main(String[] args) 
   { 
      for(int a = 1; a <= 10; a++)
      { 
         if(a == 6)
         { 
            // continue statement
            continue; 
         } 
         System.out.println(a); 
      } 
   }
}

Output:

1
2
3
4
5
7
8
9
10


Java program of continue in while and do while loop

In case of “while” and “do while” loop, if continue keyword is encountered the control moves to the boolean expression.

public class ContinueWhileDemo 
{
   public static void main(String[] args) 
   { 
      int a = 1; 
      while(a <= 10)
      { 
         if(a == 4)
         { 
            // continue statement 
            a++; 
            continue; 
         }
         System.out.println(a); 
         a++; 
      }
   }
}

Output:

1
2
3
5
6
7
8
9
10


Java program of continue in do-while loop

public class ContinueDoWhile 
{
   public static void main(String[] args) 
   { 
      int a = 1; 
      do
      { 
         if(a == 7)
         { 
            // continue statement 
            a++; 
            continue; 
         }
         System.out.println(a); 
         a++;
      }while(a <= 10);
   }
}

Output:

1
2
3
4
5
6
8
9
10