Continue and Break Statements

Continue and Break Statements

Sometimes, while using loops, we want to take the control back to the beginning of the loop or out of the loop body. For this purpose we use continue and break statements.

Break:

We often come across situations in which we need to jump out of the loop without completing the total number of iterations, the loop is supposed to run. In such situations we make use of the break statement.

For example if I want to check if I have written a program for taking 10 number input from user but terminate the program if the user enter “0” may it be the 1st input or 4th or any other.

So, in this case if the user intends to input this set {2,4,5,0,6,7,6,4,23,1} of numbers in the program, then the program will be terminated on the 4th iteration when he will enter “0”

So, here the normal execution of the program will make the loop body run for 10 times. But if the user enters “0” at any instant then all of the remaining iterations will be skipped and the control of the program will jump out of the loop body.

In the discussed case, on the fourth iteration, the program will encounter a break statement and will take the program control out of the loop body.

Syntax:

break;

Diagrammatic Representation:

Flow Chart:

Example Code:

  1. // Program to calculate the sum of a maximum of 10 numbers
  2. // If “0”is entered, the loop terminates
  3. # include <stdio.h>
  4. int main()
  5. {
  6. int i, number, sum = 0;
  7. for(i=1; i <= 10; ++i)
  8. {
  9. printf(“Enter number %d: “,i);
  10. scanf(“%lf”,&number);
  11. // If the user enters “0”, the loop ends
  12. if(number == 0)
  13. {
  14. break;
  15. }
  16. sum += number; // sum = sum + number;
  17. }
  18. printf(“Sum = %d”,sum);
  19. return 0;
  20. }

Output:

Enter number 1: 2

Enter number 2: 4

Enter number 3: 5

Enter number 4: 0

Sum = 11

Continue:

The continue statements works in the same way as break statement do; as it also interrupts the normal function of the loop. Unlike break, here the program control jumps to the beginning of the loop body.

It is used when sometimes, in the middle of the execution of loop body, we want to skip the rest of the statements in the loop body and start the new iteration.

As the loop starts and the program control goes to the loop body, in normal situations the loop body is executed specified number of times until the looping condition turns out to be false. But if we add a continue statement within the loop body, then the program will check for “continue condition” in every iteration and if that condition becomes true in any of the iteration then the program control jumps to the beginning of the loop.

Syntax:

continue;

Diagrammatic Representation:

Flow Chart:

Example Code:

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int i=1; //initializing a local variable
  5. for(i=1;i<=10;i++) //starting a loop from 1 to 10
  6. {
  7. if(i==5)
  8. { //if value of i is equal to 5, it will continue the loop
  9. continue;
  10. }
  11. printf(“%d \n”,i);
  12. } //end of for loop
  13. return 0;
  14. }

Output:

1

2

3

4

6

7

8

9

10

Number 5 is not printed in the output as in the code continue statement is activated when the value of the variable became 5 and in this case the printing statement has been skipped.