Confused with while and do-while?
simple thing- while executes the set of instructions in the loop only if the condition specified is true. On the other hand, do- while executes the set of instructions atleast once what ever the condition may be.
syntax:
while do-while
while(i<=10) do
{ {
printf("%d",i); this;
and this;
}while(this condition is true);
i++;
}
1.Example program
#include<stdio.h>
#include<conio.h>
int main()
{
while(4<2)
printf("this is while");
return 0;
}
output:
No output will be generated as the condition fails.
#include<stdio.h>
#include<conio.h>
int main()
{
do
{
printf("this is do- while");
}while(5<2);
return 0;
}
output:
this is do-while
since it is do-while the printf statement will be executed atleast once.
simple thing- while executes the set of instructions in the loop only if the condition specified is true. On the other hand, do- while executes the set of instructions atleast once what ever the condition may be.
syntax:
while do-while
while(i<=10) do
{ {
printf("%d",i); this;
and this;
}while(this condition is true);
i++;
}
1.Example program
#include<stdio.h>
#include<conio.h>
int main()
{
while(4<2)
printf("this is while");
return 0;
}
output:
No output will be generated as the condition fails.
#include<stdio.h>
#include<conio.h>
int main()
{
do
{
printf("this is do- while");
}while(5<2);
return 0;
}
output:
this is do-while
since it is do-while the printf statement will be executed atleast once.
No comments:
Post a Comment