Signup/Sign In

Java Continue Statement Programs

In this tutorial, we will learn how to use the continue statement in different scenarios. But before moving further, if you are not familiar with the concept of the break statement, then do check the article on Continue Statements in Java.

Syntax:

jump-statement;    
continue;   

Program 1: Java Continue Statement Program

In this program, we will see the implementation of the continue statement in a for loop.

Algorithm:

  1. Start
  2. Use a for loop to print the elements from 1 to 10.
  3. If 6 is encountered hen, skip that element.
  4. Print the remaining elements.
  5. Stop.

Below is the code for the same.

//Java Program to see the implementation of continue statement
import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println("The elements are: ");
       for(int i=1;i<=10;i++)
       {
           if(i==6)
           {
               continue;
           }
           System.out.println(i);
       }
    }
}


The elements are:
1
2
3
4
5
7
8
9
10

Program 2: Java Continue Statement Program

In this program, we will see the implementation of the continue statement in an inner loop.

Algorithm:

  1. Start
  2. Use two for loops.
  3. Run both the loops from 1 to 3.
  4. If the index of both the for loop matches, then use the continue statement.
  5. Print the elements.
  6. Stop.

Below is the code for the same.

//Java Program to see the implementation of continue statement
public class Main 
{  
    public static void main(String[] args) 
    {  
            //outer loop  
            for(int i=1;i<=3;i++)
            {    
                    //inner loop  
                    for(int j=1;j<=3;j++)
                    {    
                        if(i==j)
                        {    
                            //using continue statement inside inner loop  
                            continue;    
                        }    
                        //Print the elements
                        System.out.println(i+" "+j);    
                    }    
            }    
    }  
}  


1 2
1 3
2 1
2 3
3 1
3 2

Program 3: Java Continue Statement Program

In this program, we will see the implementation of the continue statement in a do-while loop.

Algorithm:

  1. Start
  2. Use a do-while loop to print the elements that are less than 10.
  3. Run the loop from 0 to 10.
  4. If the element is 5, then use the continue statement.
  5. Print the elements.
  6. Increment the value of the loop variable in each iteration.
  7. Stop.

Below is the code for the same.

//Java Program to see the implementation of continue statement
import java.util.*;
public class Main 
{  
    public static void main(String[] args) 
    {  
	    int j=0;
        do
	    {
	       if (j==5)
	       {
		      j++;
		      continue;
	       }
	       System.out.print(j+ " ");
	       j++;
           }while(j<10);
    }  
}  


0 1 2 3 4 6 7 8 9

Program 4: Java Continue Statement Program

In this program, we will see how to print the sum of all the positive entered numbers.

Algorithm:

  1. Start
  2. Declare variables for the number and sum.
  3. Initialize the sum to 0.
  4. Create an instance of the Scanner class.
  5. Use a for loop to enter the numbers.
  6. Check whether the number is positive or not.
  7. If any negative number is entered, use the continue statement.
  8. Increment the sum in each iteration.
  9. Display the sum.
  10. Stop.

Below is the code for the same.

//Java Program to implement the continue statement  
import java.util.Scanner;

public class Main 
{
  public static void main(String[] args) 
  {
    int num, sum = 0;
    // create an object of Scanner
    Scanner sc = new Scanner(System.in);

    for (int i = 1; i < 10; ++i) 
    {
      System.out.println("Enter number " + i + " : ");
      // takes input from the user
      num = sc.nextInt();

      // if number is negative
      // continue statement is executed
      if (num <= 0) {
        continue;
      }

      sum += num;
    }
    System.out.println("The sum of all the numbers  = " + sum);
    sc.close();
  }
}


Enter number 1: 7
Enter number 2: 6
Enter number 3: 5
Enter number 4: 4
Enter number 5: 8
Enter number 6: -9
Enter number 7: 1
Enter number 8: 2
Enter number 9 : 3
The sum of all the numbers = 36



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.