Does `continue` jump to the top of a `do while`?
continue jump to the top of a do while? with practical examples, diagrams, and best practices. Covers java, loops, do-while development techniques with visual explanations.Categories:
Understanding continue in Java do-while Loops

Explore how the continue statement interacts with do-while loops in Java, clarifying its flow control and common misconceptions.
The continue statement in Java is a powerful control flow mechanism used within loops. Its primary function is to skip the current iteration of a loop and proceed to the next iteration. While its behavior is straightforward in for and while loops, its interaction with do-while loops sometimes leads to confusion. This article will clarify exactly where continue jumps to within a do-while loop and provide practical examples.
The Mechanics of continue
When continue is encountered inside any loop (for, while, or do-while), the current iteration is immediately terminated. The program's control then transfers to the loop's update expression (for for loops) or directly to the loop's condition evaluation (for while and do-while loops). It's crucial to understand that continue does not exit the loop entirely; it merely skips the remaining statements in the current iteration.
How continue Works in a do-while Loop
In a do-while loop, the continue statement causes the program to jump directly to the evaluation of the loop's condition. It does not jump back to the do block's beginning. This means any statements after continue within the do block for the current iteration are skipped, and the loop's condition is checked to determine if the next iteration should execute.
flowchart TD
A[Start do-while loop] --> B{Execute 'do' block statements};
B --> C{Is 'continue' encountered?};
C -- Yes --> D{Evaluate 'while' condition};
C -- No --> E[Execute remaining 'do' block statements];
E --> D;
D -- True --> B;
D -- False --> F[Exit loop];Flowchart illustrating the behavior of continue within a do-while loop.
continue always moves to the next iteration's decision point, not necessarily the very beginning of the loop body. For do-while, this means jumping to the while condition.Practical Example: do-while with continue
Let's look at a Java example to solidify this understanding. We'll create a do-while loop that prints numbers but skips even numbers using continue.
public class DoWhileContinue {
public static void main(String[] args) {
int count = 0;
do {
count++;
if (count % 2 == 0) {
System.out.println("Skipping even number: " + count);
continue; // Jumps to 'while (count < 5)'
}
System.out.println("Processing odd number: " + count);
} while (count < 5);
System.out.println("Loop finished. Final count: " + count);
}
}
In this example:
- The loop starts,
countbecomes 1. count % 2 == 0is false, so "Processing odd number: 1" is printed.countbecomes 2.count % 2 == 0is true, so "Skipping even number: 2" is printed.continueis encountered. The program immediately jumps towhile (count < 5). The lineSystem.out.println("Processing odd number: " + count);is skipped forcount = 2.- The condition
count < 5(2 < 5) is true, so the loop continues. - This process repeats. When
countis 3, "Processing odd number: 3" is printed. Whencountis 4, "Skipping even number: 4" is printed, and thecontinueskips the print statement for odd numbers. - When
countbecomes 5, thewhile (count < 5)condition becomes false, and the loop terminates.
continue with loop variables that are incremented after the continue statement. If the increment is skipped, you might inadvertently create an infinite loop.