Difficulty: Easy
Correct Answer: 3
Explanation:
Introduction / Context:
This question tests your understanding of the control flow of a do-while loop in C, especially how many times the loop condition is evaluated. Unlike a while loop, a do-while loop always executes its body at least once and then checks the condition at the end of each iteration.
Given Data / Assumptions:
• The variable i is initialized to 0.
• The loop body increments i and prints a message.
• The condition is i < 3 and is checked at the end of each loop execution.
• The question asks specifically how many times the value of i is checked in the while condition, not how many times the body executes.
Concept / Approach:
In a do-while loop, execution always follows this pattern: execute the body once, then check the condition; if the condition is true, execute the body again, then check the condition again; and so on until the condition becomes false. The number of condition checks is equal to the number of completed iterations. For a loop that runs a fixed number of times, we can simulate the iterations and count the checks.
Step-by-Step Solution:
Step 1: Before the loop starts, i = 0.
Step 2: First iteration: execute the body, i++ makes i = 1, print "In while loop". Then check the condition i < 3: this is the first condition check (1 < 3 is true).
Step 3: Second iteration: body runs again, i++ makes i = 2, print "In while loop". Check the condition again: second condition check (2 < 3 is true).
Step 4: Third iteration: body runs again, i++ makes i = 3, print "In while loop". Check the condition once more: third condition check (3 < 3 is false).
Step 5: Because the third check is false, the loop terminates and the condition is not evaluated again.
Verification / Alternative check:
Notice that the loop body executed three times and after each execution the condition was evaluated once, totaling three checks. Another way to see it is that for a do-while loop that runs N times, the condition will be checked N times: once after each completed iteration. Since i takes on values 1, 2 and 3 inside the loop and stops when i becomes 3 and fails the condition, N is 3 and so the condition is checked three times.
Why Other Options Are Wrong:
Option A (2) would imply that the loop condition is only evaluated twice, which would stop the loop earlier than actually happens. Option C (4) would mean a fourth condition check occurs, but once the condition is false on the third check, the loop exits and no further checks are made. Option D (1) is clearly too small because the loop runs several times and must check the condition after each iteration.
Common Pitfalls:
A common mistake is to confuse the number of times the loop body executes with the number of times the condition is checked in pre-test loops like while. In do-while loops, it is easy to forget that the body executes before the first condition check. Some learners also incorrectly assume there is an extra check before entering the loop, but that applies to while, not do-while. Carefully simulating the loop with a small table of i values and checks helps avoid these off by one errors.
Final Answer:
The condition i < 3 is evaluated after each of the three iterations, so the value of i is checked 3 times.
Discussion & Comments