Do While Loop In C Language With Examples

do while:

The terminating condition in the for and while loops is always tested before the body of the loop is executed -- so of course the body of the loop may not be executed at all. In the do while statement on the other hand the statement body is always executed at least once as the condition is tested at the end of the body of the loop.

Syntax
do
{
  statement body ;
}  while ( condition ) ;
do while loop in c language

For Example : To read in a number from the keyboard until a value in the range 1 to 10 is entered.

Syntax
int i ;
do
{
  scanf( "%d\n", &i ) ;
  _flushall() ;
}   while ( i < 1 && i > 10 ) ;

In this case we know at least one number is required to be read so the do-while might be the natural choice over a normal while loop.