The do-while loop statement is also used for looping as for loop. The syntax of do-while syntax
do
{
statement
statement
statement
……..
……..
}
while(expression);
note in the do-while loop semi colon is placed after the parenthesis containing the expression
in the do-while loop the semi colon is place after the parenthesis containing the expression. in the example of do while loop first the body of the loop is execute and after expression is evaluated. if the expression is true or non zero then the body of the loop is executed and this process is executes until the expression is not false. When in the loop expression is false zero the loop terminates.
Generally the while loop is use more frequently than the do-while loop. But there are many situations the do-while loop is better to check the condition at the bottom of loop or check the condition after the body execute. For example in the next program understand the concept of do-while loop
/* program to count digit in a number*/
#include <iostream>
int main()
{
int num,count=0,rem;
std::cout<<"Enter a number";
std::cin>>num;
do
{
num=num/10;
count++;
}
while(num>0);
std::cout << "the number of digits are =" << count;
return 0;
}