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.
It is also known as entry controlled loop because the condition is checked first and then body of the loop is executed based on the condition. So if condition is false at the first trial the body of loop will never executes.

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.
It is also known as exit controlled loop because the condition is checked at last and then body of the loop is executed based on the condition. So even if condition is false at the first trial the body of loop executes at least once.  

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 

Download Projects


Download Programs