The continue
keyword in Java is used to skip the current iteration of a loop and proceed with the next iteration. It is useful when you want to skip certain parts of a loop based on a condition.
Table of Contents
- Introduction
continue
Keyword Syntax- Understanding
continue
- Examples
- Basic Usage in Loops
- Using
continue
withwhile
Loop - Using
continue
in Nested Loops
- Real-World Use Case
- Conclusion
Introduction
The continue
keyword is used within looping statements (for
, while
, do-while
) to skip the rest of the code inside the loop for the current iteration and proceed with the next iteration of the loop. It helps to bypass specific parts of a loop when a condition is met.
continue Keyword Syntax
The syntax for using the continue
keyword is simple:
continue;
Example:
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } System.out.println(i); }
Understanding continue
When the continue
statement is encountered, the control jumps to the next iteration of the loop, skipping any code that follows within the loop body for the current iteration.
Key Points:
- The
continue
statement can be used insidefor
,while
, anddo-while
loops. - It only affects the current iteration of the loop.
Examples
Basic Usage in Loops
To demonstrate the basic usage of the continue
keyword, we will print only the odd numbers between 0 and 9.
Example
public class ContinueExample { public static void main(String[] args) { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } System.out.println(i); } } }
Output:
1 3 5 7 9
Using continue
with while
Loop
The continue
keyword can also be used with a while
loop to skip certain iterations.
Example
public class ContinueWhileExample { public static void main(String[] args) { int i = 0; while (i < 10) { i++; if (i % 2 == 0) { continue; } System.out.println(i); } } }
Output:
1 3 5 7 9
Using continue
in Nested Loops
The continue
keyword can be used in nested loops to skip iterations in the inner loop.
Example
public class ContinueNestedLoops { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { continue; } System.out.println("i = " + i + ", j = " + j); } } } }
Output:
i = 1, j = 1 i = 1, j = 3 i = 2, j = 1 i = 2, j = 3 i = 3, j = 1 i = 3, j = 3
Real-World Use Case
Skipping Specific Elements in a List
In real-world applications, the continue
keyword can be useful for skipping specific elements in a list or array that meet certain conditions.
Example
import java.util.Arrays; import java.util.List; public class ContinueListExample { public static void main(String[] args) { List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve"); for (String name : names) { if (name.startsWith("D")) { continue; } System.out.println(name); } } }
Output:
Alice Bob Charlie Eve
Conclusion
The continue
keyword in Java is used for controlling the flow of loops. It allows you to skip the rest of the current iteration and move on to the next iteration. By understanding and using the continue
keyword, you can write more flexible and efficient Java programs, particularly when dealing with loops and conditions.