Recall & Review
beginner
What is the main difference between
while and do-while loops in Java?The while loop checks the condition before running the loop body, so it may not run at all if the condition is false initially. The do-while loop runs the loop body first, then checks the condition, so it always runs at least once.
Click to reveal answer
beginner
When does the condition get evaluated in a
while loop?The condition is evaluated before the loop body runs. If the condition is false at the start, the loop body does not run at all.
Click to reveal answer
beginner
When does the condition get evaluated in a
do-while loop?The condition is evaluated after the loop body runs. This means the loop body always runs at least once.
Click to reveal answer
beginner
Can a
do-while loop run zero times?No. A
do-while loop always runs the loop body at least once before checking the condition.Click to reveal answer
beginner
Which loop is better if you want to run the code at least once regardless of the condition?
The
do-while loop is better because it runs the loop body first before checking the condition.Click to reveal answer
Which loop checks the condition before executing the loop body?
✗ Incorrect
The while loop checks the condition before running the loop body.
Which loop guarantees the loop body runs at least once?
✗ Incorrect
The do-while loop runs the loop body first, then checks the condition.
If the condition is false initially, which loop will NOT run the loop body?
✗ Incorrect
The while loop checks the condition first and will skip the loop body if false.
What happens first in a do-while loop?
✗ Incorrect
The loop body executes first, then the condition is checked.
Which loop is more suitable when you want to repeat an action only if a condition is true from the start?
✗ Incorrect
The while loop only runs if the condition is true initially.
Explain in your own words the key difference between a while loop and a do-while loop.
Think about when the condition is checked in each loop.
You got /3 concepts.
Describe a real-life situation where a do-while loop would be better than a while loop.
Imagine you want to show a menu at least once before asking if the user wants to continue.
You got /3 concepts.