TechGeeks

Use of 'break' and 'continue' Statement

The break Statement

The break statement gives an early exit from for, while, and do while loop, just as from switch. A break causes the innermost enclosing loop to be exited immediately.

The continue Statement

The continue statement is somewhat similar to break, but less often used; it causes the next iteration of the enclosing for, while, or do loop to begin. In the while and do, this means that the test part is executed immediately; in the for, control passes to the increment step. The continue statement is applicable only to loops, not to switch.

Consider the following program:

#include<stdio.h>

void main( )

{

  int a,b;

  for ( a = 1 ; a <= 3 ; a++ )

{

     for ( b = 1 ; b <= 3; b++ )

      {

if ( a == b)

{
	continue ;

}

printf ( "%d\t%d\n", a,b);

     }

  }

}
 

Output
1 2
1 3
2 1
2 3
3 1
3 2

NOTE 1: when the value of a equals that of b, the continue statement takes the control to the for loop (inner) by passing rest of the statements pending execution in the for loop (inner).

The goto statement

Syntax:

goto statement label; 

Consider the following program fragmant

if (n > 12)

goto x;

goto y;

x: cost = cost * 1.05;

count = 2;

y: bill = cost * count;

Here, if the if conditions satisfies the program jumps to block labelled as x:
if not then it jumps to block labelled as y:

<< Back Next >>