Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the continue
statement is used to skip the rest of the loop's current iteration and move on to the next iteration. It is often used with for
, while
, and do-while
loops. In this tutorial, we will cover the basics of the continue
statement in Java and how to use it with different types of loops.
continue
with a for
LoopIn a for
loop, the continue
statement can be used to skip the current iteration and move on to the next one. This is particularly useful when you want to perform an action only for specific elements in a collection or array.
Example:
public class ContinueExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { // Skip even numbers if (i % 2 == 0) { continue; } System.out.println(i); } } }
Output:
1 3 5 7 9
continue
with a while
LoopSimilarly, you can use the continue
statement in a while
loop to skip the current iteration and move on to the next one.
Example:
public class ContinueExample { public static void main(String[] args) { int i = 0; while (i < 10) { i++; // Skip even numbers if (i % 2 == 0) { continue; } System.out.println(i); } } }
Output:
1 3 5 7 9
continue
with a do-while
LoopThe continue
statement can also be used in a do-while
loop to skip the current iteration and move on to the next one.
Example:
public class ContinueExample { public static void main(String[] args) { int i = 0; do { i++; // Skip even numbers if (i % 2 == 0) { continue; } System.out.println(i); } while (i < 10); } }
Output:
1 3 5 7 9
In summary, the continue
statement in Java is used to skip the rest of the current iteration in a loop and move on to the next one. It can be used with for
, while
, and do-while
loops to control the flow of the loop based on specific conditions.
Using continue statement in loops in Java
The continue
statement is used to skip the rest of the loop body and move to the next iteration.
for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip iteration when i is 3 } System.out.println("Iteration: " + i); }
Examples of Java continue statement
Here's an example where the continue
statement is used to skip odd numbers in a loop:
for (int i = 1; i <= 10; i++) { if (i % 2 != 0) { continue; // Skip odd numbers } System.out.println("Even number: " + i); }
Skipping iterations with Java continue statement
In this example, the continue
statement is used to skip iterations where the value is less than 5:
for (int i = 1; i <= 10; i++) { if (i < 5) { continue; // Skip iterations with i < 5 } System.out.println("Iteration: " + i); }
Nested loops and continue statement in Java
The continue
statement can be used in nested loops to skip the current iteration of the inner loop and move to the next iteration of the outer loop.
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { continue; // Skip inner loop iteration when j is 2 } System.out.println("i: " + i + ", j: " + j); } }
Infinite loop prevention with continue in Java
The continue
statement can be used to prevent infinite loops by adding a condition that allows the loop to skip to the next iteration.
int count = 0; while (count < 5) { if (condition) { count++; continue; // Skip to the next iteration } // Loop body }
Java labeled continue statement
Java supports labeled continue
statements, allowing you to specify which loop to continue when dealing with nested loops.
outerLoop: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { continue outerLoop; // Continue the outer loop } System.out.println("i: " + i + ", j: " + j); } }