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:
- Start
- Use a for loop to print the elements from 1 to 10.
- If 6 is encountered hen, skip that element.
- Print the remaining elements.
- 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:
- Start
- Use two for loops.
- Run both the loops from 1 to 3.
- If the index of both the for loop matches, then use the continue statement.
- Print the elements.
- 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:
- Start
- Use a do-while loop to print the elements that are less than 10.
- Run the loop from 0 to 10.
- If the element is 5, then use the continue statement.
- Print the elements.
- Increment the value of the loop variable in each iteration.
- 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:
- Start
- Declare variables for the number and sum.
- Initialize the sum to 0.
- Create an instance of the Scanner class.
- Use a for loop to enter the numbers.
- Check whether the number is positive or not.
- If any negative number is entered, use the continue statement.
- Increment the sum in each iteration.
- Display the sum.
- 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