- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
continue statement in Java with examples
Have you ever wanted to skip certain loop iterations in Java? Perhaps you encountered a specific condition that, when met, makes continuing with the rest of the loop's code for that iteration unnecessary. This is where the "continue" statement enters the picture. In this article, we'll look at the Java "continue" statement and how it can be used to optimize code flow in loops.
What is the continue statement used for in Java?
The "continue" statement in Java is used to move on to the next iteration of a loop while skipping the current one. In loops like the for, while, and do-while loops, it is frequently employed.
Java continue statement syntax
The general form or syntax of the "continue" statement in Java is as follows:
continue;
That is, the "continue" keyword followed by a semicolon makes it a "continue statement".
When the "continue" statement is encountered within a loop, the loop immediately proceeds to the next iteration, disregarding any code that follows the "continue" statement within the loop block.
Java continue statement example
Consider the following Java program as an example demonstrating the "continue" statement:
public class ContinueStatementExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // skip the iteration when i=5
}
System.out.print(i + " ");
}
}
}
1 2 3 4 6 7 8 9 10
This program iterates through the numbers from 1 to 10 using a "for" loop. The "if" clause within the loop determines whether the value of "i" at the moment equals 5. If so, the loop skips the current iteration and moves on to the next one by executing the "continue" statement.
You can see that when the value of "i" is 5, then the "System.out.print()" statement is not executed. So, excluding "5", all the values from 1 to 10 are printed.
Some additional thoughts on the continue statement:
- The continue statement is only valid within loops (i.e., for, while, and do-while loops).
- Outside of a loop, the continue statement is ineffective. It will cause a compile-time error if used.
- The loop is not terminated by the continue statement. It only skips the current iteration and moves on to the next. The loop will keep running until its termination condition is met.
break vs. continue example in Java
public class BreakVsContinueExample { public static void main(String[] args) { int i; System.out.println("Using the 'break' statement:\n"); for (i = 1; i <= 10; i++) { if (i == 5) { break; } System.out.print(i + " "); } System.out.println("\nUsing the 'continue' statement:\n"); for (i = 1; i <= 10; i++) { if (i == 5) { continue; } System.out.print(i + " "); } }
Using the 'break' statement: 1 2 3 4 Using the 'continue' statement: 1 2 3 4 6 7 8 9 10
The break statement is used in the first loop to stop the loop when "i" equals 5. As a result, it only prints the values of "i" ranging from 1 to 4.
When "i" equals 5, the second loop uses the continue statement to skip the iteration. As a result, it prints the "i" values from 1 to 4 and then from 6 to 10.
« Previous Tutorial Next Tutorial »