Continue statement in java

Let’s learn continue statement in java.

Continue statement in java

Continue keyword in java is used in many loop control structures. Use of continue keyword is, it will skip a particular iteration and moves to next iteration.

Also read – treemap in java

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

Syntax

continue;

Flow diagram:

continue statement in java

Continue in java for loop

In “for” loop if continue statement is encountered, the control will move to beginning of “for” loop, skipping current iterations and moving to next iteration. Let’s see java program,

// for loop java continue
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 while loop continue

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 do while continue

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