What is the difference between while
and do-while
loops in C?
a)
do-while
executes the loop body at least once, while
may notb)
while
executes the loop body at least once, do-while
may notc) Both execute the loop body the same way
d)
while
is used for infinite loops, do-while
is notAnswer:
a)
do-while
executes the loop body at least once, while
may notExplanation:
The primary difference between while
and do-while
loops in C is that the while
loop checks the condition before executing the loop body, meaning the loop body may not execute at all if the condition is false initially. In contrast, the do-while
loop checks the condition after executing the loop body, ensuring that the loop body is executed at least once, regardless of the condition. This makes do-while
useful when you need the loop body to execute at least once before checking the condition.
Understanding the difference between while
and do-while
loops is important for choosing the right loop structure based on the specific requirements of the task.