«Back to Home

Core Java

Topics

Continue Statement In Java

Continue Statement
 
Continue statement is sometimes required to skip some part of the loop and to continue the execution with next loop iteration. Continue statement is mainly used inside the loop helps to bypass the section of a loop and pass the control to the start of the loop to continue the execution with the next loop iteration.
 
Syntax

continue;
 
Let’s understand with the flowchart, given below.

62

Let’s see an example of continue statement, given below.
 
Code
  1. public class ContinueExm {  
  2.     public static void main(String[] args) {  
  3.         int i;  
  4.         for (i = 1; i <= 20; i++) {  
  5.             if ((i % 5) == 0) {  
  6.                 continue;  
  7.             }  
  8.             System.out.println(i);  
  9.         }  
  10.     }  
  11. }  
82

Output

83

Here, the print statement is bypassed each time when the value stored in (i) is divisible by 5.
 
Continue statement with nested loop

Continue nested loop is used only if continue statement is used inside the nested loop.
 
For example
 
Code
  1. public class ContinueExm1 {  
  2.     public static void main(String[] args) {  
  3.         for (int a = 1; a <= 3; a++) {  
  4.             for (int b = 1; b <= 3; b++) {  
  5.                 if (a == 2 && b == 2) {  
  6.                     continue;  
  7.                 }  
  8.                 System.out.println(a + " " + b);  
  9.             }  
  10.         }  
  11.     }  
  12. }  
84

Output

85
 
Summary
 
Thus, we learnt continue statement is sometimes required to skip some part of the loop and to continue the execution with next loop iteration and also learnt how to use it in Java.