Challenge - 5 Problems
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 counter-based while loop
What is the output of the following Java code?
Java
int count = 1; while (count <= 3) { System.out.print(count + " "); count++; }
Attempts:
2 left
💡 Hint
Think about how the counter changes and when the loop stops.
✗ Incorrect
The loop starts at 1 and runs while count is less than or equal to 3. It prints 1, then 2, then 3, then stops.
❓ Predict Output
intermediate2:00remaining
Counting down with a while loop
What will this Java code print?
Java
int count = 5; while (count > 2) { System.out.print(count + " "); count--; }
Attempts:
2 left
💡 Hint
Look at the condition and how the counter decreases.
✗ Incorrect
The loop runs while count is greater than 2, so it prints 5, 4, and 3, then stops before printing 2.
❓ Predict Output
advanced2:00remaining
Sum of numbers using a while loop
What is the value of sum after this code runs?
Java
int count = 1; int sum = 0; while (count <= 4) { sum += count; count++; } System.out.println(sum);
Attempts:
2 left
💡 Hint
Add numbers 1 through 4 together.
✗ Incorrect
The loop adds 1 + 2 + 3 + 4 = 10 to sum.
❓ Predict Output
advanced2:00remaining
Loop with incorrect counter update
What happens when this code runs?
Java
int count = 1; while (count <= 3) { System.out.print(count + " "); // Missing count++ here }
Attempts:
2 left
💡 Hint
What happens if the counter never changes inside the loop?
✗ Incorrect
Without increasing count, the condition is always true and the loop never ends, printing 1 forever.
🧠 Conceptual
expert2:00remaining
Number of iterations in a counter-based while loop
How many times will the loop body execute in this code?
Java
int count = 2; while (count < 10) { count += 3; }
Attempts:
2 left
💡 Hint
Trace the values: start with count=2, add 3 each time until the condition fails.
✗ Incorrect
count=2 (<10), body executes (count=5); 5<10, body (count=8); 8<10, body (count=11); 11<10 false, stop. Executes 3 times.