introduction of do-while loop

In do while loops also the loop execution is terminated on the basis of test condition. The main difference between do while loop and while loop is in do while loop the condition is tested at the end of loop body, i.e do while loop is exit controlled whereas the other two loops are entry controlled loops.

Note: In do while loop the loop body will execute at least once irrespective of test condition.

 

initialization expression;

do {

// statements

update_expression;

} while (test_expression);

 

This can be easily understand by using a small program as below:

 

#include <stdio.h>

  

int main()

{

    int i = 2; // Initialization expression

  

    do

    {

        // loop body

        printf( "Hello World\n");    

  

        // update expression

        i++;

  

    }  while (i < 1);   // test expression

  

    return 0;

}

Contributor's Info

Created:
0Comment