Continue Statement in Java

1. Introduction

The continue statement in Java is used to skip the current iteration of a loop (for, while, or do-while) and proceed to the next iteration. When a continue statement is executed, the loop does not terminate. Instead, it immediately jumps to the next iteration, bypassing any code following the continue statement within the loop body.

2. Syntax

The syntax of the continue statement is very simple. We just need to use the continue keyword followed by a semicolon (;). We can also use labeled continue statements, which we will explore later in the article.

Here, iteration is skipped as soon as the continue statement is executed, bypassing any code following the continue statement within the loop body.

3. Using continue Statement in For Loop

The continue statement is commonly used in for loops to skip the current iteration and proceed to the next iteration based on certain conditions. Let’s look at an example where we want to print only odd numbers from an array, skipping the even numbers.

Example: Print Odd Numbers

Output:

In this example, the continue statement is used to skip even numbers. When an even number is encountered, the continue statement is executed, causing the loop to skip the System.out.println statement and proceed to the next iteration.

4. Using continue Statement in While Loop

The continue statement can also be used in while loops to skip certain iterations based on a condition. Here’s an example that prints only positive numbers from a array, skipping any negative numbers.

Example: Skip Negative Numbers

Output:

In this while loop, the continue statement skips the printing of negative numbers. When a negative number is encountered, the loop increments the index and continues to the next iteration without executing the print statement.

5. Using continue Statement in Do-While Loop

The continue statement functions similarly in do-while loops, allowing certain iterations to be skipped. Here is an example that filters out zero values from a array of numbers.

Example: Ignore Zero Values

Output:

In this do-while loop, the continue statement is used to skip over zero values. If the current number is zero, the loop increments the index and proceeds to the next iteration without executing the print statement. The do-while loop ensures that the loop body is executed at least once before the condition is checked.

6. Continue in Nested Loops

When using continue in nested loops, it only impacts the immediate loop it’s in. Consider a nested loop scenario where we want to skip certain combinations:

Output:

In this nested loop example, the iteration where i == 1 and j == 1 is skipped, demonstrating how continue can be used to control the flow in complex looping structures. Please note that it impacted only inner loop, and we didn’t skip any iteration for outer loop.

What if we wanted to skip iteration for outer loop? To do that, we can use labeled continue statements.

7. Labeled continue Statement

A label is an identifier followed by a colon (:) placed before a loop or block statement. When a continue statement specifies a label, it skips an iteration for the loop marked with that label, not just the nearest loop.

Consider a scenario where we have nested loops and want to skip to the next iteration of an outer loop based on a condition inside an inner loop.

Output:

In this example, whenever j equals 2, the continue label; statement is executed. This causes the program to skip the remaining iterations of the inner loop and continue with the next iteration of the outer loop labeled label. As a result, the output shows that the inner loop’s iterations are skipped whenever j == 2.

8. Conclusion

The continue statement in Java is a versatile tool for managing loop execution, allowing developers to skip certain iterations based on specific conditions. Through practical examples, we’ve seen how continue can be applied in both simple and nested loops to control the flow of execution. We’ve also explored labeled continue statements and their functionality

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *