Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Understanding the Break Statement
2.1.
Break in Loops
2.2.
Break in Switch Statement
3.
Frequently Asked Questions
3.1.
What does the break statement do in C++?
3.2.
Can we use break in if statement?
3.3.
Does break terminate the program?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

C++ Break Statement

Author Gunjan Batra
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Greetings, aspiring programmer! It's time to break into a new topic: the break statement in C++. This function is a control statement that plays a significant role in controlling the flow of loops and switch cases.

C++ break statement

Understanding the Break Statement

The break statement in C++ is used to stop the execution of the smallest enclosing loop or switch statement and transfer control to the next statement following the loop or switch.

It comes in handy when you need to exit prematurely from a loop or a switch statement, based on certain conditions. The most common scenario where you might use it is when searching for an item in an array or list. If you find the item, there's no need to continue searching, right? That's where the break statement shines.

Break in Loops

The break statement can be used in for, while, and do...while loops. Here's a basic example of using break in a for loop:

#include <iostream>
 
int main() {
    for (int i = 0; i < 10; i++) {
        std::cout << i << "\n";
        if (i == 5) {
            break;
        }
    }
    return 0;
}

Output

Output

In this example, the loop will only print the numbers from 0 to 5. As soon as i becomes 5, the break statement is executed and the loop is terminated.

Break in Switch Statement

In a switch statement, a break is used to prevent the "fall through" of cases. This means that once a case is matched, the program won't execute the rest of the cases below it. Here's a simple example:

#include <iostream>


int main() {
    char grade = 'B';


    switch(grade) {
        case 'A':
            std::cout << "Excellent!\n";
            break;
        case 'B':
        case 'C':
            std::cout << "Well done\n";
            break;
        case 'D':
            std::cout << "Passed\n";
            break;
        case 'F':
            std::cout << "Try again\n";
            break;
        default:
            std::cout << "Invalid grade\n";
    }
    
    return 0;
}

Output

In this example, the output will be "Well done". The break statement prevents the execution from continuing to the next cases after the matching one.

Output
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What does the break statement do in C++?

The break statement is used to exit the current loop or switch statement and transfer control to the next statement.

Can we use break in if statement?

No, the break statement is not applicable in if statements. It's primarily used in loops and switch statements.

Does break terminate the program?

No, the break statement only terminates the loop or switch statement in which it is present. The program continues to execute from the statement following the loop or switch.

Conclusion

Mastering the break statement in C++ allows you to control the flow of your program more effectively, especially in situations where you need to exit a loop or switch statement prematurely based on specific conditions. It's an essential tool in your C++ toolkit that can save both processing time and resources. Remember, with great power comes great responsibility – use the break statement wisely! Happy coding!

Recommended articles:


We hope this blog helped you to understand the concept of the Member Function in C++. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Happy Learning!! 
 

Previous article
Decision Making in C / C++ (if , if..else, Nested if, if-else-if )
Next article
Do While Loop In C++
Live masterclass