In what sequence the initialization, testing and execution of body is done…
2022
In what sequence the initialization, testing and execution of body is done while using do-while loop?
A. Commenting
B. Execution of the body
C. Initialisation
D. Testing the condition
Choose the correct answer from the following
- A.
D,B,C
- B.
D,C,B
- C.
C,A,B
- D.
C,B,D
Attempted by 677 students.
Show answer & explanation
Correct answer: D
Answer: The correct sequence is Initialisation, Execution of the body, Testing the condition.
Key point: In a do-while loop the body always runs at least once because the condition is checked after the body executes.
Initialisation: Set up loop control variable(s) before the loop starts.
Execution of the body: The loop body executes first in a do-while construct.
Testing the condition: After the body runs, the loop condition is evaluated; if true, the loop repeats.
Example (pseudocode):
initialise i
do {
// body executes here
} while (condition);
Therefore the correct ordering is: Initialisation, Execution of the body, Testing the condition.
A video solution is available for this question — log in and enroll to watch it.