Challenge - 5 Problems
Do–while Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple do–while loop
What is the output of the following Java code?
Java
int count = 1; do { System.out.print(count + " "); count++; } while (count <= 3);
Attempts:
2 left
💡 Hint
Remember, do–while executes the loop body at least once before checking the condition.
✗ Incorrect
The loop starts with count = 1, prints it, then increments. It repeats while count <= 3, so it prints 1, 2, and 3.
❓ Predict Output
intermediate2:00remaining
Value of variable after do–while loop
What is the value of variable x after this code runs?
Java
int x = 5; do { x -= 2; } while (x > 0);
Attempts:
2 left
💡 Hint
Check how x changes each loop and when the loop stops.
✗ Incorrect
x starts at 5, then subtracts 2 each time: 5->3->1->-1. The loop stops when x <= 0, so final x is -1.
🔧 Debug
advanced2:00remaining
Identify the error in this do–while loop
What error does this code produce when compiled?
Java
int i = 0; do { System.out.println(i); i++ } while (i < 3);
Attempts:
2 left
💡 Hint
Look carefully at the line before the while condition.
✗ Incorrect
The line 'i++' is missing a semicolon, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Output with break inside do–while loop
What is the output of this code?
Java
int n = 1; do { if (n == 3) break; System.out.print(n + " "); n++; } while (n <= 5);
Attempts:
2 left
💡 Hint
The break stops the loop when n equals 3.
✗ Incorrect
The loop prints 1 and 2, then when n is 3, it breaks before printing.
🧠 Conceptual
expert2:00remaining
Minimum iterations of do–while vs while loop
Which statement is true about do–while loops compared to while loops?
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop type.
✗ Incorrect
A do–while loop checks the condition after running the body once, so it always runs at least once. A while loop checks before running.