How to use break statement in C / C++
In this tutorial, we will learn about the syntax and how to use break
statement in C / C++. The break
statement is used in loops such as: for
, while
, do… while
and switch..case
statement.
Before learning about the break
statement, you need to learn the usage of:
- How to use for loop in C / C++.
- How to use while loop in C / C++.
- How to use do...while loop in C / C++.
- How to use if - else statement in C / C++.
Syntax and its working of break statement in C / C++
In the C and C++ programming languages, the break
statement is used to exit a loop when it is encountered.
Syntax of break
statement in C / C++:
break;
As you can see in the image above, when encountering a break
statement, the program will exit the loop and execute the statements below the loop.
Break statement in C / C++ with for loop
We can use break
statement to immediately exit the for
loop in some special cases. See the example below:
Example: Using break
statement in C / C++ with for
loop.
C++ programming:
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { // break condition if (i == 3) { break; } cout << i << endl; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { for (int i = 1; i <= 5; i++) { // break condition if (i == 3) { break; } printf("%d\n",i); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Explain: In this program, we display numbers from 1 to 5. However, the results only return 1 and 2. Because when printing to the value 3, a break
statement is encountered, so the loop is exited.
Break statement in C / C++ with while loop
To better understand how to use the break
statement in the while
loop, see the example below:
Example: Using break
statement in C / C++ with while
loop.
- Program to find the sum of positive numbers.
- If the user enters a negative numbers, break ends the loop.
- The negative number entered is not added to sum.
C++ programming:
#include <iostream> using namespace std; int main() { int number; int sum = 0; while (true) { // take input from the user cout << "Enter a number: "; cin >> number; // break condition if (number < 0) { break; } // add all positive numbers sum += number; } // display the sum cout << "The sum is " << sum << endl; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int number; int sum = 0; while (true) { // take input from the user printf("Enter a number: "); scanf("%d", &number); // break condition if (number < 0) { break; } // add all positive numbers sum += number; } // display the sum printf("The sum is %d\n",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Break statement in C / C++ with do...while loop
Also with the above example, we do the do while
loop as follows:
C++ programming:
#include <iostream> using namespace std; int main() { int number; int sum = 0; do{ // take input from the user cout << "Enter a number: "; cin >> number; // break condition if (number < 0) { break; } // add all positive numbers sum += number; } while (number >= 0); // display the sum cout << "The sum is " << sum << endl; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int number; int sum = 0; do{ // take input from the user printf("Enter a number: "); scanf("%d", &number); // break condition if (number < 0) { break; } // add all positive numbers sum += number; } while (number >= 0); // display the sum printf("The sum is %d\n",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Break statement in C / C++ with switch...case statement
To illustrate how to use break
statement in C / C++ with switch...case
statement, we have the following example:
Example: Displays the day of the week when the user enters a natural number.
C++ programming:
#include <iostream> using namespace std; int main() { int number; int n; cout<<"Enter number n: "; cin>>n; switch(n){ case 2: cout<<"This is Monday!"; break; case 3: cout<<"This is Tuesday!"; break; case 4: cout<<"This is Wednesday!"; break; case 5: cout<<"This is Thursday!"; break; case 6: cout<<"This is Friday!"; break; case 7: cout<<"This is Saturday!"; break; case 8: cout<<"This is Sunday!"; break; default: cout<<"It's not exist!"; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int number; int n; printf("Enter number n: "); scanf("%d", &n); switch(n){ case 2: printf("This is Monday!"); break; case 3: printf("This is Tuesday!"); break; case 4: printf("This is Wednesday!"); break; case 5: printf("This is Thursday!"; break; case 6: printf("This is Friday!"); break; case 7: printf("This is Saturday!"); break; case 8: printf("This is Sunday!"); break; default: printf("It's not exist!"); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output: