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 this C++ code using a do–while loop?
C++
int i = 1; do { std::cout << i << " "; i++; } while (i <= 3);
Attempts:
2 left
💡 Hint
Remember, do–while executes the loop body before checking the condition.
✗ Incorrect
The loop prints i starting from 1, then increments i. It repeats while i is less or equal to 3, so it prints 1 2 3.
❓ Predict Output
intermediate2:00remaining
Do–while loop with condition false initially
What will this C++ program print?
C++
int x = 5; do { std::cout << x << " "; x++; } while (x < 5);
Attempts:
2 left
💡 Hint
The loop runs once before checking the condition.
✗ Incorrect
Even though x < 5 is false at start, do–while runs once, printing 5, then stops.
🔧 Debug
advanced2:00remaining
Identify the error in this do–while loop
What error will this code cause?
C++
int count = 0; do { std::cout << count << " "; count++; } while (count < 3);
Attempts:
2 left
💡 Hint
Check punctuation carefully in C++.
✗ Incorrect
The line 'count++' is missing a semicolon, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Counting down with do–while loop
What does this code print?
C++
int n = 3; do { std::cout << n << " "; n--; } while (n > 0);
Attempts:
2 left
💡 Hint
The loop runs while n is greater than zero.
✗ Incorrect
Starts at 3, prints 3, then 2, then 1, stops before printing 0.
🧠 Conceptual
expert2:00remaining
Behavior difference between while and do–while loops
Which statement best describes the difference between a while loop and a do–while loop in C++?
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop.
✗ Incorrect
A do–while loop checks the condition after running the body, so it runs at least once. A while loop checks before running, so it may skip the body.