do-while is a condition control loop.The do-while loop is used to carry out looping operation,in which a statement or group of statements is/are executed repetitively until some logical condition has been satisfied.
do-while is also called "exit-control loop",because in do-while loop first the statement get executed and then the condition is checked,hence the do-while always execute at least one's.
In the do-while loop, there must be at least one such statement within the body of loop which changes the looping condition,during the execution of loop otherwise an infinite loop may occur.
do
statement;
while(condition);
or,
do
{statement-1;
statement-2;
-
-
statement-n;
}while(condition);
Example-:
Write a program to find sum of series of terms: 2+4+6+8+ - - -n
#include<stdio.h>
#include<conio.h>
main()
{int i=1,term=0,s=0;
printf("Enter the term");
scanf("%d",&term);
do{s=s+(2*i);
i++;
}while(i<=term);
printf("\n sum=%d",s);
getch();
}
Output-:Enter the term 3
sum=12
ads
subscribe tech programiz
ReplyDelete