It is also repetitive control structure and executes the body of the loop once irrespective of the condition, then it checks the condition and continues the execution until the condition becomes false.
Syntax:
do
{
. . . . . .
body of the loop;
. . . . . .
}
while(condition);
here the statements within the body the loop is executed once then it checks for the condition, if it is true, then it executes body until the condition becomes false.
Example:
do
{
n=n+1;
cout<<>
Difference between while loop and do while loop
Example for sum of natural numbers.
void main()
{
int i,sum=0;
i=1;
do
{
sum=sum+i;
i++;
}
while(i<=15);
cout<<>
getch();
}
No comments:
Post a Comment