Sunday, October 30, 2011

The continue Statement : C programing The Loop Control Structure

In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed. The keyword continue allows us to do this. When continue  is encountered inside any loop, control automatically passes to the beginning of the loop.
A continue is usually associated with an if. As an example, let's consider the following program.

main( )
{
      int   i, j ;

      for ( i = 1 ; i <= 2 ; i++ )
 {
            for ( j = 1 ; j <= 2 ; j++ )
  {
                  if ( i == j )
    continue ;

                  printf ( "\n%d %d\n", i, j ) ;
  }
 }
}

The output of the above program would be...

1 2 
2 1 

Note that when the value of i equals that of j, the continue statement takes the control to the for loop (inner) bypassing rest of
the statements pending execution in the for loop (inner).

No comments:

Post a Comment