Until now, we have learned to execute statements based on conditions using if…else statement. We also learned to execute repetitive statements using for loop, while loop and do…while loop.
In this post I will explain the use and importance of break statement in program flow control.
Let us consider a repetitive task to send email to 100 employees. Suppose after sending 10 mails, suddenly internet disconnected. What action you will perform at that time? You can either attempt a try to send rest 90 emails, which will result in error. Alternatively, you can terminate the mailing process with a warning or error message. Definitely, it is better to postpone or terminate the mailing process for now than sending mails further that will result in error. In C programming, to terminate immediately from a loop or switch, we make use of break statement.
break is jump statement used to terminate a switch or loop on some desired condition. On execution, it immediately transfer program control outside the body of loop or switch. In the case of nested switch or loop, it terminates the innermost switch or loop.
Syntax of break statement
break;Flowchart of break statement

How to use break statement
You must follow some rules while using break statement.
- You must write
breakstatement inside a loop orswitch. Use ofbreakkeyword outside theswitchor loop will result in compilation error. breakstatement is generally used with condition(if statements) inside loop.- In case of nested loop or
switch,breakonly terminates the innermost loop orswitch. - Use of
breakinside switch…case will transfer the program control outside theswitch. breakstatement only terminate program control from nearest matched loop orswitch.
Working of break statement
break statement terminate program control from inner switch or loop. Below are some working examples of a break statement.
- How
breakstatement works inside aswitchcase.

- How
breakstatement works inside aforloop.

- How
breakstatement works inside awhileloop.

- How
breakstatement works inside ado...whileloop.

- How
breakstatement works with nested loop.

Example program to demonstrate break statement
Let us write a C program to input number from user and check whether the given number is prime or not.
/**
* C program to check prime number
*/
#include <stdio.h>
int main()
{
/* Variable declarations */
int num, isPrime, i;
/* Input number from user */
printf("Enter any number: ");
scanf("%d", &num);
/* Initially assume that the number is prime */
isPrime = 1;
for(i=2; i<num; i++)
{
/*
* If number is divided by any number
* between 2 to n. Then the given number
* is not prime.
*/
if(num % i == 0)
{
/*
* Number is not prime.
* Hence, set prime as 0
*/
isPrime = 0;
/*
* Number is not prime, no need to check further.
* Hence terminate from loop.
* Using break here will terminate from loop not from if
*/
break;
}
}
/* Check the prime flag value */
if(isPrime == 1)
{
printf("Number is prime number.");
}
else
{
printf("Number is composite number.");
}
return 0;
}Output –
Enter any number: 7
Number is prime number.Read a detailed explanation and discussion of the above program.
Read more – Program to check prime number.




