Recall & Review
beginner
What is a do–while loop in Java?
A do–while loop is a control flow statement that executes a block of code at least once, and then repeats the loop as long as a given condition is true.
Click to reveal answer
beginner
How does a do–while loop differ from a while loop?
A do–while loop runs the code block first, then checks the condition. A while loop checks the condition first before running the code block. So, do–while always runs at least once.
Click to reveal answer
beginner
Write the basic syntax of a do–while loop in Java.
do {
// code to run
} while (condition);
Click to reveal answer
intermediate
Why might you choose a do–while loop over a while loop?
You choose a do–while loop when you want the code inside the loop to run at least once, regardless of the condition.
Click to reveal answer
beginner
What happens if the condition in a do–while loop is false on the first check?
The code inside the do block still runs once before the condition is checked. After that, the loop stops because the condition is false.
Click to reveal answer
What is guaranteed when using a do–while loop?
✗ Incorrect
A do–while loop always runs the code block once before checking the condition.
Which part of the do–while loop checks the condition?
✗ Incorrect
The condition is checked after the do block finishes, in the while statement.
What is the correct way to end a do–while loop in Java?
✗ Incorrect
A do–while loop ends with a semicolon after the while(condition); statement.
If the condition is false initially, how many times does the do–while loop run?
✗ Incorrect
The do block runs once before the condition is checked, so it runs once even if the condition is false.
Which loop is best when you want to run the code at least once?
✗ Incorrect
The do–while loop guarantees the code runs at least once.
Explain how a do–while loop works and when you would use it.
Think about the order of execution and the condition check.
You got /3 concepts.
Write a simple Java do–while loop that prints numbers 1 to 3.
Start with int i = 1; then print i and increase it inside the loop.
You got /4 concepts.