The break Statement

The break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost enclosing loop or switch to be exited immediately. When break is encountered inside any loop, control automatically passes to the first statement after the loop.

Consider the following example;
main( )
int i = 1 , j = 1 ;
while ( i++ <= 100 )
while ( j++ <= 200 )
 if ( j == 150 )
 break ;
 printf ( "%d %d\n", i, j );

In this program when j equals 150, break takes the control outside the inner while only,
since it is placed inside the inner while.

The continue Statement

The continue statement is related 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 applies only to loops, not to switch.

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) by passing rest of the statements pending execution in the for loop

The goto statement

Kernighan and Ritchie refer to the goto statement as “infinitely abusable” and suggest that it “be used sparingly, if at all. The goto statement causes your program to jump to a different location, rather than execute the next statement in sequence. The format of the goto statement is;

goto statement label;

Consider the following program fragment

if (size > 12)
goto a;
goto b;
a: cost = cost * 1.05;
flag = 2;
b: bill = cost * flag;

Here, if the if conditions satisfies the program jumps to block labelled as a: if not then it
jumps to block labelled as b:.

Share with : Share on Linkedin Share on Twitter Share on WhatsApp Share on Facebook