Which of the following statements about loop execution is true?
2010
Which of the following statements about loop execution is true?
Answer: B. The body of a do … while loop is executed at least once. — ConceptAn entry-controlled loop tests its condition before executing the body, so its body may run zero times. An exit-controlled loop executes its body…
- A.
The body of a while loop is executed at least once.
- B.
The body of a do … while loop is executed at least once.
- C.
The body of a do … while loop is executed zero or more times.
- D.
A for loop can never be used in place of a while loop.
Attempted by 93 students.
Show answer & explanation
Correct answer: B
Concept
An entry-controlled loop tests its condition before executing the body, so its body may run zero times.
An exit-controlled loop executes its body before testing the condition, so its body runs at least once.
Application
In a do … while loop, control enters the body before the first condition check.
After the body has run once, the condition is evaluated. Further iterations occur only while the condition remains true.
Cross-check
If the initial condition is false, a while loop skips its body, whereas a do … while loop completes one iteration before stopping.
Contrast
“A while loop executes at least once” overlooks the condition check that occurs before the body.
“A do … while loop executes zero or more times” gives the wrong minimum; its first test occurs after one execution.
“A for loop can never replace a while loop” is too absolute: while (condition) { body } can be written as for (; condition; ) { body }, with the for loop’s initialization and update clauses omitted.
Result
The body of a do … while loop is executed at least once.