C++ break

🛑 C++ break Statement – Exit Loops Instantly

The break statement in C++ is your emergency exit door. 🚪

It lets you instantly leave a loop or switch statement when a certain condition is met—no questions asked!

🧠 Syntax of break

break;

Just write break; where you want to exit the loop or switch block.

🔧 Example: Break a for Loop

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 5) {
            break;
        }
        cout << "i = " << i << endl;
    }
    return 0;
}

Try It Now

Output will be:

i = 1  
i = 2  
i = 3  
i = 4

🔧 Example: break in switch

#include <iostream>
using namespace std;

int main() {
    int day = 2;
    switch (day) {
        case 1:
            cout << "Sunday" << endl;
            break;
        case 2:
            cout << "Monday" << endl;
            break;
        default:
            cout << "Other Day" << endl;
    }
    return 0;
}

Try It Now

🎯 When to Use break?

  • To stop a loop early when a condition is met
  • To exit a switch block after a case runs
  • To avoid unnecessary iterations or checks

The break statement helps you stay in control and exit at the right moment—like a smart ninja! 🥷💨