Introduction
The do...while loop is similar to the while loop, but it checks the conditional expression only after the repetition part is executed. When the expression is evaluated to be false, the repetition part is not executed. Thus it is guaranteed that the repetition part is executed at least once.
Program
#include
main()
{
int i,n; //the
scanf("%d",&n);
i = 0;
do // statement A
{
printf("the numbers are %d \n",i);
i = i +1;
}while( i }/*5the numbers are 0the numbers are 1the numbers are 2the numbers are 3the numbers are 4*/Explanation
1. Statement A indicates the do...while loop.
2. The general form of the do...while loop is
3. do
4. {
5. repetition part;
6. } while (expr);
When the do...while loop is executed, first the repetition part is executed, and then the conditional expression is evaluated। If the conditional expression is evaluated as true, the repetition part is executed again. Thus the condition is evaluated after each iteration. In the case of a normal while loop, the condition is evaluated before making the iteration.
7. The loop is terminated when the condition is evaluated to be false.
Point to Remember
The do...while loop is used when you want to make at least one iteration. The condition should be checked after each iteration.
No comments:
Post a Comment