Java continue Keyword

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

  1. Introduction
  2. continue Keyword Syntax
  3. Understanding continue
  4. Examples
    • Basic Usage in Loops
    • Using continue with while Loop
    • Using continue in Nested Loops
  5. Real-World Use Case
  6. 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 inside for, while, and do-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.

Leave a Comment

Scroll to Top