what is the purpose of do…while statement?

examcolour Q&ACategory: computer sciencewhat is the purpose of do…while statement?
Rajashree asked 3 months ago

What is the purpose of do…while statement? How dose it differ form the while statement?

1 Answers
Best Answer
contributor Staff answered 3 months ago

do-while statement or do-while loop
The purpose of do…while statement/loop is
A do-while loop is used to execute and repeat a block of statement depending on a condition. It has the following form.
do
{  ________________
<statement block>
_________________
}

while(<condition>);

where <condition> is a relational or logical expression which will have the value true or false.
Example
i = 1;
do
{
s = s + i ;
p = p * i ;
i++;
}

while (i <= 10);

When this statement is executed, the computer will execute the statement block irrespective of the value of the condition. At the end of the statement block, the condition is evaluated. If the value of the condition is true the statement block is executed again and is repeated until the condition fales.

A while loop is used to execute and repeat a statement block depending on a condition which is evaluated at the beginning of the loop that’s why while loop differs from a do-while loop 

Your Answer

1 + 13 =