java
for loop
iteration
break statement
continue statement

GoTo Next Iteration in For Loop in java

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In the realm of programming, loops are one of the most fundamental constructs used for iteration. A for loop in Java is especially useful when the number of iterations is known beforehand. However, there are times when you might want to skip certain iterations based on particular conditions. Java provides several mechanisms to influence the flow of a for loop, one of which is using the continue statement to effectively move to the "next iteration". This article delves into the intricacies of this concept, providing technical explanations, examples, and additional subtopics for a thorough understanding.

Understanding the continue Statement

In Java, the continue statement is used inside loops to skip the current iteration and proceed to the next iteration of the loop. When the continue statement is executed, the program control jumps immediately to the loop's increment expression (in a for loop), thus bypassing any subsequent code in the current loop's body.

Syntax

The syntax of the continue statement is straightforward:

java
continue;

Using continue in a for Loop

Here's a practical example illustrating the use of the continue statement in a for loop:

java
1public class ContinueExample {
2    public static void main(String[] args) {
3        for (int i = 1; i <= 10; i++) {
4            if (i % 2 == 0) {
5                continue;  // Skip the rest of the loop for even numbers
6            }
7            System.out.println(i);  // Print only when the number is odd
8        }
9    }
10}

Output:

 
11
23
35
47
59

In the above example, the loop iterates from 1 to 10. The continue statement is executed when the number is even, causing the loop to skip printing the number and effectively jump to the next iteration.

Additional Details and Considerations

Differences Between continue and break

While both continue and break statements alter the flow of loops, they have key differences:

  • continue: This statement causes the loop to skip the rest of its body and immediately jump to the next iteration.
  • break: This immediately terminates the loop, exiting out of it completely.

Here’s a table summarizing these differences:

StatementFunctionalityTypical Use Case
continueSkips the current iteration and moves to the next oneSkipping specific iterations based on a condition
breakExits the loop completelyStopping the loop prematurely when a certain condition is met

continue in Different Types of Loops

While this article primarily focuses on for loops, it's worth noting that the continue statement can be used in other types of loops as well, such as while and do-while loops.

Considerations for Nested Loops

In scenarios involving nested loops, the continue statement affects only the loop immediately containing it. If you need to influence outer loops, Java's label feature can be employed.

java
1public class NestedLoopExample {
2    public static void main(String[] args) {
3        outer: for (int i = 0; i < 5; i++) {
4            for (int j = 0; j < 5; j++) {
5                if (j == 3) {
6                    continue outer;  // Direct control to the next iteration of the outer loop
7                }
8                System.out.println("i = " + i + ", j = " + j);
9            }
10        }
11    }
12}

Output:

 
1i = 0, j = 0
2i = 0, j = 1
3i = 0, j = 2
4i = 1, j = 0
5i = 1, j = 1
6i = 1, j = 2
7i = 2, j = 0
8i = 2, j = 1
9i = 2, j = 2
10i = 3, j = 0
11i = 3, j = 1
12i = 3, j = 2
13i = 4, j = 0
14i = 4, j = 1
15i = 4, j = 2

In this example, the continue outer; statement causes control to jump to the next iteration of the outer loop when j == 3.

Conclusion

The continue statement in Java is a powerful tool for controlling loop flow, allowing developers to refine iteration processes by skipping elements that meet certain conditions. By using continue judiciously, you can make your loops more efficient and your code cleaner and more readable. It can be employed in diverse contexts, from simple for loops to complex nested loop structures, making it an indispensable part of a Java programmer's toolkit. Understanding how to correctly leverage the continue statement can greatly enhance your ability to write effective and efficient Java code.


Course illustration
Course illustration

All Rights Reserved.