Sunday, October 30, 2011

The do-while Loop : C programing The Loop Control Structure

The do-while loop looks like this:

do
{
 this ;
      and this ;
      and this ;
      and this ;
} while ( this condition is true ) ;

There is a minor difference between the working of while and do-while  loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this, the do-while tests the condition after having executed the statements within the
loop. Figure 3.5 would clarify the execution of do-while loop still further



This means that do-while  would execute its statements at least once, even if the condition fails for the first 
time. The while, on the other hand will not execute its statements if the condition fails for the first time. This difference is brought about more clearly by the following program.

main( )
{
  while ( 4 < 1 )
    printf ( "Hello there \n") ;
}
 
Here, since the condition fails the first time itself, the printf( ) will
not get executed at all. Let's now write the same program using a
do-while loop.

main( )
{
 do
 {
            printf ( "Hello there \n") ;
      } while ( 4 < 1 ) ;
}

In this program the printf( ) would be executed once, since first the body of the loop is executed and then the condition is tested. 

There are some occasions when we want to execute a loop at least once no matter what. This is illustrated in the following example: 

break and continue are used with do-while just as they would be in a while or a for loop. A break takes you out of the do-while bypassing the conditional test. A continue  sends you straight to the test at the end of the loop. 

No comments:

Post a Comment