You need to prompt a user for a positive integer and keep asking until they give one. Which loop structure handles this most cleanly?
AA while loop: check the condition first, then prompt inside the loop
BA for loop: iterate a fixed number of times until the input is valid
CA do-while loop: prompt inside the body, then check if the input is valid
DA while loop with the prompt written both before the loop and inside it
A do-while loop is the cleanest fit because you must prompt at least once to get any input — you can't test validity before the user has entered anything. With a do-while, you prompt in the body, then test the result. If invalid, the loop repeats. Option D (while loop with duplicated prompt) works but violates DRY — you're writing the same code twice because you need at least one execution before testing. This 'bootstrapping problem' is exactly what do-while is designed to solve.
Question 2 Multiple Choice
What happens when a do-while loop's condition is false the very first time it is evaluated?
AThe loop body never executes — identical behavior to a while loop with a false condition
BThe loop body executes exactly once, then the loop terminates
CA runtime error occurs because the condition was never initialized
DThe loop body executes, then the condition is re-evaluated repeatedly until true
This is the defining difference between do-while and while. In a do-while loop, the condition is checked AFTER the body executes — so the body always runs at least once, regardless of the condition. Even if the condition is false on first evaluation, the loop body has already completed one iteration. This guaranteed-first-execution is exactly why do-while is the right tool when one pass is unconditionally required.
Question 3 True / False
A do-while loop and a while loop that contain identical bodies and conditions will generally produce identical results.
TTrue
FFalse
Answer: False
They differ when the condition is false before the first iteration. A while loop with an initially false condition executes zero times. A do-while loop with the same condition executes once. This distinction matters in input validation: a while loop requires a 'seed' value or duplicated prompt before entering the loop, while a do-while handles the first iteration naturally. If the condition is always true on first entry, they produce the same results — but that is a special case, not the general rule.
Question 4 True / False
The do-while loop always executes its body at least once, regardless of whether the condition is true or false.
TTrue
FFalse
Answer: True
This is the core guarantee of the do-while construct. Because the condition check comes after the body (post-test), the body must execute before the condition is ever evaluated. The condition can be false from the start — the loop still completes one iteration before checking. This makes do-while useful precisely in situations where zero iterations is not a valid outcome — you always need at least one run of the code.
Question 5 Short Answer
Why is a do-while loop more natural than a while loop for input validation? What problem does it solve?
Think about your answer, then reveal below.
Model answer: Input validation requires asking the user for input before you can test whether it's valid — you need at least one execution of the prompt unconditionally. A while loop checks the condition first, but the condition depends on input you haven't gathered yet. The workaround is to prompt before the loop and again inside it, duplicating code. A do-while eliminates this by executing the body (which includes the prompt) first, then checking validity. The first prompt is automatic because the body always runs at least once.
This 'bootstrapping problem' — needing to run the body before you can meaningfully test the condition — is the canonical use case for do-while. The same logic applies to menu-driven programs: the menu must appear at least once before the user can choose to quit. Whenever the first iteration is unconditionally required, do-while is the natural fit.