Updated July 6, 2023
Introduction to Break Statement in C
Break Statement in C is a loop control statement that is used to terminate the loop. There are two usages and the given statement is explained below.
- Inside a Loop: If the break statement is using inside a loop along with the if statement then if the condition becomes true the loop is immediately terminated and the next statement after the loop starts executing by the program control.
- Inside a Switch Case: If Break Statement in C is using inside a switch case after each switch case then the break statement terminates a case after executing the case.
In general the break statements we used in the situations where we need to stop the loop execution based on the condition or not sure how many times the loop is to be iterate. If the break statements using inside the nested loop, then the break statement breaks the inner loop and starts executing the statement after the inner loop of the program control continue to the outer loop.
Syntax of the Break Statement:
// inside switch case or loop
break;
Flowchart
Figure – Flowchart of the break statement
Examples to Implement Break Statement in C
Below are the different examples to implement in C language:
Program Example #1
Break statement inside the for a loop
#include<stdio.h>
int main ()
{
int co;
for(co = 0; co < 10; co++)
{
printf("loop %d\n",co);
if(co == 6)
break;
}
printf("\n",co);
printf("The loop terminat at co = %d", co);
return 0;
}
The output of the above code:
Program Example #2
Break statement inside the switch case
#include <stdio.h>
void main()
{
char opt;
printf("Enter the option \“A\”, \“B\”, \“C\” or \“D\” :");
scanf( "%c", &opt );
switch (opt)
{
case 'B':
printf( "You have entered option B " );
break;
case 'A':
printf( "You have entered option A " );
break;
case 'D':
printf( "You have entered option D ");
break;
case 'C':
printf( "You have entered option C " );
break;
default:
printf ( "You have not entered option A, B, C, or D, wrong input ");
}
}
The output of the above code:
Program Example #3
Break statement inside the nested loop, in nested case, it breaks only the inner loop, but not outer loop, as can be understood by the code
#include<stdio.h>
int main()
{
int inner, outer;
for( outer=0; outer <= 5; outer++)
{
for(inner=0; inner <= 5; inner++)
{
printf("outer = %d & inner = %d loop running\n",outer,inner);
if(outer == 3 && inner == 2)
{
break;
}
}
printf("\n");
}
return 0;
}
The output of the above code:
So as in the above output, when outer = 3 & inner = 2 the inner loop break and the execution continue to the outer loop for 4th iteration.
Program Example #4
Break statement inside while loop
Consider the following example to use the break statement inside while loop.
#include <stdio.h>
int main ()
{
int co = 0;
while(co < 10)
{
printf("loop %d\n",co);
if(co == 6)
break;
else
co = co +1;
}
printf("\n",co);
printf("The loop terminat at co = %d", co);
return 0;
}
The output of the above code:
Program Example #5
Break statement inside the do-while loop
Consider the following example to use the break statement with a do-while loop.
#include<stdio.h>
int main ()
{
int co = 0;
do
{
printf("loop %d\n",co);
if(co == 6)
break;
else
co = co +1;
}while(co < 10);
printf("\n",co);
printf("The loop terminat at co = %d", co);
return 0;
}
The output of the above code:
Program Example #6
Next, we write some of the c program examples with the break statement
#include <stdio.h>
void linearsearch(int a[], int key)
{
// search for key by traverse to array one by one element in loop
for (int indx = 0; indx <= (sizeof(a) / sizeof(a[0])) ; indx++) {
if (a[indx] == key) {
printf( "Element present at position : %d ", (indx + 1));
// stop execution of the loop by break
break;
}
}
}
int main() {
int a[] = { 11, 22, 33, 44, 55, 65 };
// search for key 22
int key = 22;
// linearserch function call to serach key 22
linearsearch(a, key);
return 0;
}
The output of the above code:
Program Example #7
Next, we write the c program example to accept the character from the user and count the number of vowels entered by the user.
#include<stdio.h>
int main ()
{
char inp, opt;
int count=0,i;
for(i=1;i<=10;i++)
{
printf( "Enter a character \n" );
scanf( "%c", &inp );
fflush( stdin );
if( inp=='a' || inp=='e' || inp=='i' || inp=='o' || inp=='u' )
{
count = count+1;
}
}
printf("The total number of vowels entered are %d ", count);
return 0;
}
The output of the above code:
Program Example #8
The above code we rewrite with break statement as in below c program
#include <stdio.h>
#include<stdio.h>
int main ()
{
char inp, opt;
int count=0,i;
for(i=1;i<=10;i++)
{
printf( "Enter a character \n" );
printf( "Enter ! if you want to stop \n" );
scanf( "%c", &inp );
fflush(stdin);
if( inp=='a' || inp=='e' || inp=='i' || inp=='o' || inp=='u' )
{
count = count+1;
}
if( inp=='!' )
{
break;
}
}
printf("The total number of vowels entered are %d ", count);
return 0;
}
The output of the above code:
Conclusion
The break keyword used brings out the program control from loop execution. There are two usages of the break statement in C programming one is inside a loop and the second is inside a switch case.
Recommended Articles
This is a guide to Break Statement in C. Here we discuss Syntax, flowchart, and usage of break statement in C along with different examples and code implementation. You can also go through our other suggested articles to learn more–