Q62.Marks: +2.0UGC NET Paper 2: Computer Science 11 March 2023
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
1.D, B, C
2.D, C, B
3.C, A, B
4.C, B, D✓ Correct
Solution
The correct answer is C, B, D
Key Points
Initialization (C):
This is the first step that usually takes place before entering the loop. We initialize the variables in this step. For instance, let's say we have a counter or iterator; we define and set it to an initial value (like int i = 0;) before we start the loop. Note that this step may not always be 'inside' the loop construct but is logically the first step related to the loop.
Execution of the Body (B):
After initialization step, control enters the loop. In the case of the 'do-while' loop, the loop’s body gets executed at least once, regardless of whether the condition is true or false. This is because the condition is tested after the execution of the loop's body. So the 'do' part of 'do-while' is actually executing the body of the loop.
Testing the Condition (D):
After the body of the loop has been executed, the condition in the 'while' part is tested. If this condition evaluates to true, the control jumps back to the start of the loop and executes the loop's body again. If it evaluates to false, the loop is exited, and the control moves to the next part of the program following the loop.