While Loop �& �Do-While Loop
While Loop
while(condition)
{ statement(s); }
Properties of While loop
Program
#include <stdio.h>
int main()
{
int a = 10;
while( a < 20 )
{ printf("value of a: %d\n", a);
a++;
}
return(0);
}
Do-While Loop
do{
statement(s);
}while( condition );
Program
#include <stdio.h>
int main ()
{int a = 10;
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
Difference between For & While
For Loop | While Loop |
The number of iteration are Known. | The number of iteration are unknown. |
A condition is in the form of relational expression. | A condition is in the form of non-zero value or expression |
The initialization may exist both- outside the loop or in the loop statement. | The initialization, in this case, always occurs outside the loop. |
The increment occurs only after the execution of the statement(s). | We can perform the increment both- after or before the execution of the given statement(s). |
We use the for loop when the increment and initialization are very simple. | We use the while loop in the case of a complex initialization. |
Syntax | Syntax |
e.g | e.g |
HOME WORK
1. While & Do-While
2. For & do-While