While Loop in C
Syntax
while (condition) { body of loop; } While loop first check the condition. If condition is true then the statements written inside body of loop are executed. After executing statements inside body of loop again the condition is checked.
This process is repeated until condition specified within while loop becomes false.
As the condition becomes false the statements inside body of loop are not executed
and the statements followed by the while loop is executed. Example
int i; i=1; while(i<=10) { printf(" %d \n",i); i++; } it will display 1 2 3 4 5 6 7 8 9 10 |
do ... while loop in C
Syntax
do { body of loop; } while (condition); In do … while loop first statements written inside body of the loop are executed. After executing statements inside body of the loop the condition is checked. If condition is true then the statements written inside body of loop are executed again.
This process is repeated until condition specified within do … while loop becomes
false. As the condition becomes false the statements inside body of loop is not
executed and the statements followed by the do … while loop is executed. Example
int i; i=1; do { printf(" %d \n",i); i++; }while(i<=10); it will display 1 2 3 4 5 6 7 8 9 10 |