Courseiva
Control Flow and LoopsmediumMultiple ChoiceObjective-mapped

Continue in While Loop: Variable Increment

What is the output of the following code?

int i = 0;
while (i < 5) {
    if (i == 3) {

i++; continue;

}

System.out.print(i + " "); i++;

}

Quick Answer

The answer is 0 1 2 4. This output occurs because the continue statement, paired with a variable increment inside the if block, forces the loop to skip the remainder of the current iteration when i equals 3, jumping directly to the next loop condition check. The key technical concept here is that the continue statement does not automatically advance the loop counter; you must explicitly increment i before the continue, otherwise the loop would become infinite. On the Oracle Java Foundations 1Z0-811 exam, this pattern tests your understanding of loop control flow and the common trap of forgetting to increment before a continue, which leads to an endless loop. A reliable memory tip is "increment before you skip"—always ensure your loop variable is updated before the continue keyword to avoid infinite loops.

⚠ Common exam trap

Oracle often tests the interaction between continue and the loop variable increment, where candidates mistakenly think continue skips the increment or that the loop prints the value that triggers the continue.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

0 1 2 4

The while loop iterates while i < 5. When i equals 3, the if condition triggers, incrementing i to 4 and then using continue to skip the print statement for that iteration. Thus, 3 is never printed, and the loop prints 0, 1, 2, and then 4 before i becomes 5 and the loop ends.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • 0 1 2 4

    Why this is correct

    Correctly skips 3 due to continue.

  • 0 1 2 3

    Why it's wrong here

    Missing 4 and includes 3.

  • 0 1 2 4 5

    Why it's wrong here

    Loop stops at i<5, so 5 not printed.

  • 0 1 2 3 4

    Why it's wrong here

    Does not skip 3.

About these practice questions

One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

3 more ways this is tested on 1Z0-811

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. What is the output of the code? ```java for (int i = 0; i < 5; i++) { if (i == 2) { continue; } System.out.print(i + " "); } ```

medium
  • A.0 1 2 3 4
  • B.1 3 4
  • C.0 1 2 3
  • D.0 1 3 4

Why D: The code uses a for loop that iterates from i = 0 to i < 5 (i.e., 0 through 4). Inside the loop, there is an if condition that checks if i equals 2. When i == 2, the continue statement is executed, which skips the rest of that iteration, so the System.out.print statement is not executed for i = 2. Therefore, the loop prints 0, 1, 3, and 4, each followed by a space. Option D correctly lists this output.

Variation 2. A developer writes: for(int i=0; i<10; i++) { if(i%2==0) continue; System.out.print(i); }. What is the output?

hard
  • A.0123456789
  • B.13579
  • C.02468
  • D.123456789

Why B: The loop iterates from i=0 to i=9. The `continue` statement skips the rest of the loop body when the condition `i%2==0` is true (i.e., when i is even). Therefore, only odd values of i (1, 3, 5, 7, 9) are printed, producing the output '13579'. Option B is correct.

Variation 3. In a Java method, a developer needs to skip the current iteration and move to the next when a certain condition is met inside a for loop. Which statement should be used?

easy
  • A.return;
  • B.exit;
  • C.continue;
  • D.break;

Why C: The `continue` statement in Java immediately skips the remaining code in the current iteration of a loop and proceeds to the next iteration. This is exactly what the developer needs when a condition is met inside a `for` loop to move to the next cycle without executing further statements in the current iteration.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.