Programming in C: Unit I (c): Decision Control and Looping Statements

The Break and Continue Statements

with Example C Programs

In C, the break statement is used to terminate the execution of the nearest enclosing loop in which it appears. We have already seen its usage in the switch statement.

THE BREAK AND CONTINUE STATEMENTS

break Statement

In C, the break statement is used to terminate the execution of the nearest enclosing loop in which it appears. We have already seen its usage in the switch statement. The break statement is widely used with for loop, while loop, and do-while loop. When compiler encounters a break statement, the control passes to the statement that follows the loop in which the break statement appears. Its syntax is quite simple, just type keyword break followed with a semicolon.

Programming Tip: The break statement is used to terminate the execution of the nearest enclosing loop in which it appears.

break;

In switch statement if the break statement is missing then every case from the matched case label till the end of the switch, including the default, is executed. This example given below shows the manner in which break statement is used to terminate the statement in which it is embedded.

#include <stdio.h>

int main()

{

int i=1;

while (i<=10)

{

if (i= =5)

break;

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

i = i + 1;

}

return 0;

}

Note that the code is meant to print first 10 numbers using a while loop, but it will actually print only numbers from 1 to 4. As soon as i becomes equal to 5, the break statement is executed and the control jumps to the statement following the while loop.

Hence, the break statement is used to exit a loop from any point within its body, bypassing its normal termination expression. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control is passed to the next statement following the loop. Figure 3.9 shows the transfer of control when the break statement is encountered.

continue Statement

Like the break statement, the continue statement can only appear in the body of a loop. When the compiler encounters a continue statement then the rest of the statements in the loop are skipped and the control is unconditionally transferred to the loop-continuation portion of the nearest enclosing loop. Its syntax is quite simple, just type keyword continue followed with a semicolon.

Programming Tip: When the compiler encounters a continue statement, then rest of the statements in the loop are skipped and the control is unconditionally transferred to the loop-continuation portion of the nearest loop.

continue;

Again like the break statement, the continue statement cannot be used without an enclosing for, while, or do-while statement. When the continue statement is encountered in the while and do-while loops, the control is transferred to the code that tests the controlling expression. However, if placed within a for loop, the continue statement causes a branch to the code that updates the loop variable. For example, look at the following code.

#include <stdio.h>

int main()

{

int i;

for (i=1; i<= 10; i++)

{

if (i= =5)

continue;

printf("\t %d", i);

}

return 0;

}

Programming Tip: As far as possible, try not to use goto, break, and continue statements as they violate the rules of structured programming.

The code given here is meant to print numbers from 1 to 10. But as soon as i becomes equal to 5, the continue statement is encountered, so rest of the statements in the for loop are skipped and the control passes to the expression that incre- ments the value of i. The output of this program would thus be

1 2 3 4 6 7 8 9 10

(Note that there is no 5 in the series. It could not be printed, as continue caused early incrementation of i and skipping of the statement that printed the value of i on screen).


Hence, we conclude that the continue statement is somewhat the opposite of the break statement. It forces the next iteration of the loop to take place, skipping any code in between itself and the test condition of the loop. The continue statement is usually used to restart a state- ment sequence when an error occurs. Look at the program code given below that demonstrates the use of break and continue statements.

65.Write a program to calculate square root of a number.

#include <stdio.h>

#include <math.h>

int main()

{

int num;

do

{

printf("\n Enter any number. Enter 999 to stop: ");

scanf("%d", &num);

if (num = = 999)

 break; // quit the loop

if (num < 0)

{

printf("\n Square root of negative numbers is not defined");

continue; // skip the following statements

}

printf("\n The square root of %d is %f", num, sqrt (num));

} while (1);

return 0;

}

Programming in C: Unit I (c): Decision Control and Looping Statements : Tag: : with Example C Programs - The Break and Continue Statements