while and do...while
while checks a condition before its body; do...while checks after its body, so it runs at least once.
Decide whether the first attempt is allowed before choosing a loop form.
See the mechanism
0falseFollow one example
The while condition is false initially, so it performs zero attempts. do...while performs its body once before checking that same false condition. A loop must make progress toward stopping unless it intentionally waits on an external condition; an unchanged true condition can run forever.
Predict, change and check
Predict: If both conditions use attempts < 2, how many increments occur before each printed line?
Test your explanation: Run that variation, then reset attempts separately before each loop to compare the loop forms fairly. Never test an intentionally endless loop in the main page thread.
The tasks below ask you to explain that result and apply the idea to different inputs.
Apply and distinguish
Test-before and test-after are different contracts
while checks before entering its body, so it may execute zero times. do...while checks after the body, so it runs at least once. A menu prompt that must be shown once and an empty collection that needs no processing have different requirements; choose the structure accordingly.
Every loop needs a credible termination argument: the condition changes, a finite input is consumed, or control exits deliberately. If a counter is updated only in one branch, another branch may loop forever. Test zero, one and several iterations, and use the runner's timeout as a last defense rather than as your loop's intended stopping mechanism.
Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.