EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Break Statement in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Break Statement in C

Break Statement in C

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of the Break Statement:

// inside switch case or loop
break;

Flowchart of Break Statement in C

Figure – Flowchart of the break statement

Flowchart of Break Statement in C

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:

terminant

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:

options

entered option

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

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

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

Break Statement in C

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:

loop terminant

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:

loops

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:

element

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:

Break Statement in C

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:

Break Statement in C

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–

  1. Types of Websites
  2. What is Web Hosting?
  3. Web Application Security
  4. What is Selenium Web Driver?
  5. Complete Guide to Control Statement in C++
  6. Types of Control Statement in JavaScript
  7. Overview and Different Control Statement in Java
  8. Guide to Top 6 Examples of Break in PHP

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More