Continue statement in java

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

continue statement in java

Continue statement in java

In java, “continue” is a keyword used in many loop control structures to jump to next iteration.

Also read – java constructor example

It continues the flow of program and skipping the rest of code.

Syntax

continue;


What is the function of continue?

Continue keyword causes loop to immediately jump to next iteration of the loop. Hence skipping statements inside loop. Let’s see an example,

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

Output:

1
2
3
4
5
7
8
9
10


Example: Continue in while loop

Here continue keyword is used inside a while loop,

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


Example: Continue in do-while loop

We can also see the use 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

Related Posts