The value of condition for " do.....while loop " is placed at the end of the loop.Like :
do
{
statement;
} while (condition);
Watch the program carefully :
#include<stdio.h>
#include<conio.h>
main()
{
int i=0;
do
{
printf(" %d ",i);
i++
} while (i<5);
getch();
}
OUTPUT :
0,1,2,3,4
Program Analysis :
-> do
{
printf(" %d ",i);
................................
................................
Here the value of i will be shown first.
-> i++;
}while(i<5);
In this step,the value of i will be increased by 1 and value will be 0 to 1 and in the mean time condition will also be checked.As i<5 so condition will be true and printf function will be executed and 1 will be shown.
-> In this way when the value of i will be 5 than the condition will be false because i can't be equal to 5 according to the condition(i<5).So the loop will end.
Post a Comment