Q: What is the difference between a while loop and a do-while loop in C++?
-
A
A while loop always executes at least once
-
B
A do-while loop checks the condition before the loop
-
C
A do-while loop executes at least once
-
D
A while loop can only be used with integers
C
Answer:
C
Explanation:
The key difference between a while loop and a do-while loop is that a do-while loop guarantees that the loop's body will execute at least once, even if the condition is false from the beginning. In a while loop, the condition is checked first, so if the condition is false initially, the loop body may never execute. This makes the do-while loop useful for situations where you need the loop to run at least once, regardless of the condition. In contrast, a while loop only runs if the condition is true.
Related Topic:
Share Above MCQ