Skip to main content

Practical-10

Write a Java Program that implements the use of continue statement.

Introdution

This program demonstrates the use of a continue statement in Java. A continue statement causes the program to skip any remaining code in the current loop iteration and start another iteration. It will jump out of the loop and continue with the statements after the loop. The program contains a simple for loop which prints 10 numbers except 5 and 6.

Code
public class Main {

public static void main(String[] args) {

int i;
System.out.println("Printing 10 numbers except 5 and 6");
for (i = 1; i <= 10; i++) {
if (i == 5 || i == 6) {
continue;
}
System.out.println(i);
}
}
}
Output
Printing 10 numbers except 5 and 6
1
2
3
4
7
8
9
10
Explanation

The continue statement inside the for loop causes the program to skip any remaining code in the loop when the condition (i == 5 || i == 6) is true. The program then continues from the beginning of the loop and prints the numbers from 1 to 10 except 5 and 6.