Java Continue Statement

What is the Continue statement?

The continue keyword is generally used to end the current iteration in a (for, while, do…while etc).loop and jumps to the next iteration.
The continue statement is often used in decision-making statements such as (if… else statement).

Example 1: Continue in for loop

Suppose you want to print all odd numbers from 1 to 15

Java Code

public class Solution{
// Main driver method

   public static void main(String[] args){

        for(int i = 1 ; i<= 15 ; i++){

             if(i % 2 == 0) // Condition check
                  continue; // Skipping numbers which are even
             System.out.print(i);
       }
    }
}

Output: 1 3 5 7 9 11 13 15

Explanation: 

  • In the above code, we are printing only odd numbers by skipping even numbers with the help of the continue-statement.
  • The continue statement in the above code skips the “ith” iteration of the for loop when the value of “ i ” is even and prints odd numbers when the value of “ i ” is Odd.

Example 2: Continue Statement in While loop

Suppose you want to print the sum of even numbers from 1 to 11.

Java Code

public class Solution{
// Main driver method
   public static void main(String[] args){
// Creating & initializing variables	
     int sum  = 0;
     int n = 11;
      while(n-- >= 1){
        // decrementing value of n by 1 in while loop
    
  	   if(n % 2 != 0) // Condition check
      	 continue; // skipping odd numbers 
  	   else
    	     sum += n;  // Adding even numbers to the sum variable
 	 }
   System.out.println("The sum of even numbers up to 11 is "+ sum);
  }
}

Output: The sum of even numbers up to 11 is 30

Explanation:

  • With the help of the continue-statement, we skipped odd numbers and calculated the sum of even numbers.
  • The continue-statement skipped all those numbers which were not divisible by 2.

Example 3: Continue Statement in do-while loop

Suppose we want to print numbers from 0 to 10 by incrementing each number by 2 and skipping the “ ith ” iteration when the number is 4.

Java Code

public class Solution {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating and Initializing a variable
        int i = 0;
 
        // Do-While loop for iteration
        do {
            if (i == 4) {
                 i = i+2;
                 continue; // skipping number 4
            }
     
            // Printing to showcase continue affect
            System.out.print(i);
 
            // Incrementing variable by 2
            i += 2;
         // Condition check

        } while (i <= 10);
    } 
}

Output: 0 2 6 8 10

Explanation:

  • With the help of continue-statement, we skipped the “ith” iteration when the number was 4 and incremented the iterating variable  “ i ” by 2.

Special thanks to Abbas Hussain Muzammil for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article