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
break
statement inside a loop orswitch
. Use ofbreak
keyword outside theswitch
or loop will result in compilation error. break
statement is generally used with condition(if statements) inside loop.- In case of nested loop or
switch
,break
only terminates the innermost loop orswitch
. - Use of
break
inside switch…case will transfer the program control outside theswitch
. break
statement 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
break
statement works inside aswitch
case.
- How
break
statement works inside afor
loop.
- How
break
statement works inside awhile
loop.
- How
break
statement works inside ado...while
loop.
- How
break
statement 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.