Java Reference Library →

continue Keyword Usage and Examples

continue Keyword Usage and Examples

Learn how to use the Java continue keyword to control loop execution. This beginner-friendly guide covers syntax, logic flow, and real-world examples.

This reference explains continue Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

continue Keyword in Java

The continue keyword in Java is used to skip the current iteration of a loop and jump to the next one. It gives you finer control over the flow of loops, especially when certain conditions should interrupt the current cycle without exiting the loop entirely.

Why Use continue ?

Imagine you’re looping over a list of numbers, but want to skip processing any negative values. Or perhaps you're filtering strings and want to avoid ones that are empty. The continue keyword makes your code cleaner and avoids deeply nested if-statements.

Syntax

continue;

That’s it. The keyword stands alone and ends with a semicolon. It’s typically placed inside an if condition inside loops.

How continue Works in a Loop

When the Java runtime encounters continue, it immediately skips to the next iteration of the nearest enclosing loop. Any code written after continue; in the current iteration is not executed.

Example 1: Skipping Even Numbers in a for Loop

public class ContinueExample1 {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue; // Skip even numbers
            }
            System.out.println("Odd number: " + i);
        }
    }
}
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9

Explanation

Java · example discussed here

public class ContinueExample1 {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue; // Skip even numbers
            }
            System.out.println("Odd number: " + i);
        }
    }
}

Whenever i % 2 == 0 evaluates to true (i.e., the number is even), the continue statement is triggered. This skips the System.out.println() call and moves on to the next iteration.

Example 2: continue in a while Loop

public class ContinueExample2 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            i++;
            if (i == 5) {
                continue; // Skip when i is 5
            }
            System.out.println("Value: " + i);
        }
    }
}
Value: 1
Value: 2
Value: 3
Value: 4
Value: 6
Value: 7
Value: 8
Value: 9
Value: 10

Explanation

Java · example discussed here

public class ContinueExample2 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            i++;
            if (i == 5) {
                continue; // Skip when i is 5
            }
            System.out.println("Value: " + i);
        }
    }
}

When i becomes 5, continue is executed. The System.out.println() line is skipped for that specific iteration.

Example 3: continue with a for-each Loop

public class ContinueExample3 {
    public static void main(String[] args) {
        String[] fruits = {"Apple", "", "Banana", "Mango", ""};
        for (String fruit : fruits) {
            if (fruit.isEmpty()) {
                continue; // Skip empty strings
            }
            System.out.println("Fruit: " + fruit);
        }
    }
}
Fruit: Apple
Fruit: Banana
Fruit: Mango

Explanation

Java · example discussed here

public class ContinueExample3 {
    public static void main(String[] args) {
        String[] fruits = {"Apple", "", "Banana", "Mango", ""};
        for (String fruit : fruits) {
            if (fruit.isEmpty()) {
                continue; // Skip empty strings
            }
            System.out.println("Fruit: " + fruit);
        }
    }
}

We use continue to skip printing any empty strings. This pattern is useful in data cleansing or filtering scenarios.

Common Mistakes with continue

  • Infinite Loops: If used carelessly in while or do-while loops, continue can cause the loop condition never to be updated. Always ensure variables are updated before the continue.
  • Unreachable Code: Placing code after a continue statement inside a block will make that code unreachable for that execution path.

When to Use continue — Best Practices

The continue keyword is ideal when:

  • You want to filter or skip elements conditionally inside a loop.
  • You aim to make your loop logic more readable by avoiding deeply nested conditions.
  • You want to enforce early exits from unnecessary computations in a loop cycle.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →