Recall & Review
beginner
What is a do–while loop in C++?
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 specified 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 C++.
do {
// code to run
} while (condition);
Click to reveal answer
intermediate
Why is the semicolon required after the while(condition) in a do–while loop?
The semicolon marks the end of the do–while statement. It is required because the do–while loop is a single statement that includes the condition check after the block.
Click to reveal answer
beginner
What will happen if the condition in a do–while loop is initially false?
The code inside the do block will still run once before the condition is checked and the loop stops.
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.
Where is the condition checked in a do–while loop?
✗ Incorrect
In a do–while loop, the condition is checked after the loop body executes.
Which of the following is the correct syntax for a do–while loop in C++?
✗ Incorrect
The correct syntax is do { /* code */ } while (condition); with a semicolon at the end.
What happens if the condition in a do–while loop is false after the first run?
✗ Incorrect
If the condition is false after the first run, the loop stops.
Why might you choose a do–while loop over a while loop?
✗ Incorrect
A do–while loop is used when you want the code to run at least once regardless of the condition.
Explain how a do–while loop works and when you would use it.
Think about the order of execution and condition checking.
You got /3 concepts.
Write a simple do–while loop in C++ that prints numbers 1 to 3.
Start with a counter variable and loop until it reaches 3.
You got /4 concepts.