# Loops — 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.

break;

## Example Code:

// Program to calculate the sum of a maximum of 10 numbers

// If “0”is entered, the loop terminates

# include <stdio.h>

int main()

{

int i, number, sum = 0;

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

{

printf(“Enter number %d: “,i);

scanf(“%lf”,&number);

// If the user enters “0”, the loop ends

if(number == 0)

{

break;

}

sum += number; // sum = sum + number;

}

printf(“Sum = %d”,sum);

return 0;

}

## 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.

continue;

## Example Code:

#include<stdio.h>

int main()

{

int i=1; //initializing a local variable

for(i=1;i<=10;i++) //starting a loop from 1 to 10

{

if(i==5)

{ //if value of i is equal to 5, it will continue the loop

continue;

}

printf(“%d \n”,i);

} //end of for loop

return 0;

}

## 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.