Sunday 2 September 2012

December 2010 - Question No 14

Which one of the following sentences is true ?

(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.

Ans:-B

Explanation:- Since this question is a very simple and easy one i am posting the answer for this also today. Otherwise my idea was to take one question, explore it, analyze and then try to present the answer. Although many of the readers must be knowing this answer i still want to emphasize that the body of a do…while loop is executed at least once, since the condition is tested at the bottom. In case of while loop and for loop, the condition is tested at the top of the loop, so there are chances that they may not be executed at all. But that is not the case with the do…while loop. A do…while loop will be executed at least once.

No comments:

Post a Comment