Break Statement in C

Break Statement in C
Techiio-author
Written by Shuvhojit DebFebruary 14, 2022
9 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, you'll know what the break statement in C is.

Introduction

Break Statement in C is a loop control proclamation that is utilized to end the loop. There are two utilizations and the given assertion is clarified underneath.

  • Inside a Loop: If the break statement is utilized inside a loop alongside the if statement then, at that point, if the condition turns out to be valid the loop is quickly ended and the following statement after the loop begins executing by the program control.
  • Inside a Switch Case: If Break Statement in C is utilized inside a switch case after each switch case then the break statement ends a case after executing the case.

Overall the break statements we utilized in the circumstances where we want to stop the loop execution in light of the condition or not certain how frequently the loop is to be repeated. If the break statements utilizing inside the settled loop, then, at that point, the break statement breaks the inward loop and starts executing the statement after the internal loop of the program control proceed to the external loop.

Syntax:

// inside switch case or loop
break;

Flowchart

blogpost

Examples to Implement Break Statement in C

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;
}

Output:

blogpost

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 ");
}
}

Output:

blogpost

blogpost

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;
}

Output:

blogpost

So as in the above output, when outer = 3 & inner = 2 the inner loop break, and the execution continue to the outer loop for the 4th iteration.

Example-4

Break statement inside while loop

Consider the following example to use the break statement inside the 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;
}

Output:

blogpost

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;
}

Output:

blogpost

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;
}

Output:

blogpost

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;
}

Output:

blogpost

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;
}

Output:

blogpost

C Language
Break Statement
Control Statement
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance