Lesson 31 of 60 – break Statement in C++
52%

break Statement in C++

The break statement is used to immediately stop the execution of a loop or a switch statement. When C++ reaches the break statement, it exits the current loop or switch block and continues execution from the statement after it.

Note: The break statement is commonly used with for, while, do-while loops and switch statements.

1. What is the break Statement?

The break statement terminates the nearest enclosing loop or switch statement immediately.

break;

After break executes, program control moves to the statement following the loop or switch.

2. Syntax of break

break;

The break statement contains only the keyword break followed by a semicolon.

3. break with for Loop

for (int i = 1; i <= 10; i++) {

    if (i == 5) {
        break;
    }

    std::cout << i << std::endl;
}

Output:

1
2
3
4

When i becomes 5, the break statement terminates the loop.

4. How break Works

Consider the following loop:

for (int i = 1; i <= 10; i++) {

    if (i == 6) {
        break;
    }

    std::cout << i << std::endl;
}

The execution is:

  1. Print 1.
  2. Print 2.
  3. Print 3.
  4. Print 4.
  5. Print 5.
  6. When i == 6, break executes.
  7. The loop terminates.

5. break with while Loop

int i = 1;

while (i <= 10) {

    if (i == 5) {
        break;
    }

    std::cout << i << std::endl;

    i++;
}

The loop stops when i becomes 5.

6. break with do-while Loop

int i = 1;

do {

    if (i == 5) {
        break;
    }

    std::cout << i << std::endl;

    i++;

} while (i <= 10);

The break statement immediately terminates the do-while loop.

7. break in switch Statement

int choice = 2;

switch (choice) {

    case 1:
        std::cout << "One";
        break;

    case 2:
        std::cout << "Two";
        break;

    case 3:
        std::cout << "Three";
        break;

    default:
        std::cout << "Invalid choice";
}

In a switch statement, break prevents execution from falling into the next case.

8. break in a Simple Search

int target = 7;

for (int i = 1; i <= 10; i++) {

    if (i == target) {

        std::cout << "Number found";
        break;
    }
}

Once the target number is found, there is no need to continue the loop.

9. Stop When a Number is Found

int numbers[] = {10, 20, 30, 40, 50};

for (int i = 0; i < 5; i++) {

    if (numbers[i] == 30) {

        std::cout << "30 found";
        break;
    }
}

The loop stops immediately after finding 30.

10. break Based on User Input

int number;

while (true) {

    std::cout << "Enter a number: ";
    std::cin >> number;

    if (number == 0) {
        break;
    }

    std::cout << "You entered: "
              << number
              << std::endl;
}

Entering 0 terminates the loop.

11. Infinite Loop with break

A loop with true as its condition is an infinite loop. A break statement can provide a controlled exit.

while (true) {

    std::cout << "Enter 0 to stop: ";

    int number;
    std::cin >> number;

    if (number == 0) {
        break;
    }
}

12. break with if Statement

The break statement is commonly placed inside an if statement.

for (int i = 1; i <= 10; i++) {

    if (i > 5) {
        break;
    }

    std::cout << i << std::endl;
}

The if statement determines when the loop should stop.

13. break with Character Input

char choice;

while (true) {

    std::cout << "Enter y to continue or n to stop: ";
    std::cin >> choice;

    if (choice == 'n') {
        break;
    }
}

The loop terminates when the user enters n.

14. break with Multiplication Table

int number = 5;

for (int i = 1; i <= 10; i++) {

    if (i == 6) {
        break;
    }

    std::cout << number
              << " x "
              << i
              << " = "
              << number * i
              << std::endl;
}

Only the first five lines of the multiplication table are displayed.

15. break with Array

int numbers[] = {5, 10, 15, 20, 25};

for (int i = 0; i < 5; i++) {

    std::cout << numbers[i] << std::endl;

    if (numbers[i] == 15) {
        break;
    }
}

The array traversal stops after 15 is displayed.

16. break with String

#include <iostream>
#include <string>

int main() {

    std::string text = "HELLO";

    for (int i = 0; i < text.length(); i++) {

        if (text[i] == 'L') {
            break;
        }

        std::cout << text[i];
    }

    return 0;
}

The loop stops when the first L is found.

17. break with Nested Loops

When break is used inside nested loops, it terminates only the nearest enclosing loop.

for (int i = 1; i <= 3; i++) {

    for (int j = 1; j <= 5; j++) {

        if (j == 3) {
            break;
        }

        std::cout << j << " ";
    }

    std::cout << std::endl;
}

Here, break terminates the inner loop, not the outer loop.

18. break Does Not Stop All Loops

for (int i = 1; i <= 3; i++) {

    for (int j = 1; j <= 5; j++) {

        if (j == 2) {
            break;
        }

        std::cout << j << " ";
    }

    std::cout << "Outer loop: "
              << i << std::endl;
}

The inner loop stops at 2, but the outer loop continues.

19. break in do-while Menu

int choice;

do {

    std::cout << "1. Start" << std::endl;
    std::cout << "2. Exit" << std::endl;

    std::cin >> choice;

    if (choice == 2) {
        break;
    }

} while (true);

std::cout << "Program ended";

20. break for Input Validation

int marks;

while (true) {

    std::cout << "Enter marks: ";
    std::cin >> marks;

    if (marks >= 0 && marks <= 100) {
        break;
    }

    std::cout << "Invalid marks"
              << std::endl;
}

std::cout << "Valid marks entered";

The loop ends only after valid marks are entered.

21. break in a Login Attempt

#include <iostream>
#include <string>

int main() {

    std::string password;

    for (int attempt = 1; attempt <= 3; attempt++) {

        std::cout << "Enter password: ";
        std::cin >> password;

        if (password == "1234") {

            std::cout << "Login successful";
            break;
        }

        std::cout << "Wrong password"
                  << std::endl;
    }

    return 0;
}

Once the correct password is entered, the loop stops.

22. break in a Search Program

int numbers[] = {12, 25, 36, 48, 50};
int target = 36;
bool found = false;

for (int i = 0; i < 5; i++) {

    if (numbers[i] == target) {

        found = true;
        break;
    }
}

if (found) {
    std::cout << "Number found";
}
else {
    std::cout << "Number not found";
}

The break statement avoids unnecessary searching after the target is found.

23. break with switch and Loop

int choice;

while (true) {

    std::cout << "1. Add" << std::endl;
    std::cout << "2. Exit" << std::endl;

    std::cin >> choice;

    switch (choice) {

        case 1:
            std::cout << "Add selected";
            break;

        case 2:
            std::cout << "Exit selected";
            return 0;

        default:
            std::cout << "Invalid choice";
    }

    std::cout << std::endl;
}

Here, the break inside the switch exits the switch case. The return statement ends the complete program.

24. break vs continue

break continue
Terminates the loop. Skips the current iteration.
Execution moves outside the loop. Loop continues with its next iteration.
Used when no more iterations are needed. Used when the current iteration should be skipped.
// break
if (i == 5) {
    break;
}

// continue
if (i == 5) {
    continue;
}

25. break vs return

Both break and return can stop a loop, but they have different purposes.

for (int i = 1; i <= 10; i++) {

    if (i == 5) {
        break;
    }
}

std::cout << "Program continues";

With break, only the loop is terminated and the program continues after the loop.

int test() {

    for (int i = 1; i <= 10; i++) {

        if (i == 5) {
            return 0;
        }
    }

    return 1;
}

A return exits the current function.

26. Common Mistakes with break

  • Using break outside a loop or switch.
  • Expecting break to terminate every nested loop.
  • Confusing break with continue.
  • Putting break in the wrong condition.
  • Forgetting that break terminates only the nearest enclosing loop.
  • Using break when a normal loop condition would be clearer.
for (int i = 1; i <= 10; i++) {

    if (i == 5) {
        break;
    }

    std::cout << i;
}

27. Practical Number Search

#include <iostream>

int main() {

    int target;
    int numbers[] = {10, 20, 30, 40, 50};

    std::cout << "Enter number to search: ";
    std::cin >> target;

    for (int i = 0; i < 5; i++) {

        if (numbers[i] == target) {

            std::cout << "Number found";
            break;
        }

        if (i == 4) {
            std::cout << "Number not found";
        }
    }

    return 0;
}

The loop stops immediately when the target is found.

28. Practical ATM Exit Example

#include <iostream>

int main() {

    int choice;

    while (true) {

        std::cout << std::endl;
        std::cout << "1. Check Balance" << std::endl;
        std::cout << "2. Deposit" << std::endl;
        std::cout << "3. Withdraw" << std::endl;
        std::cout << "4. Exit" << std::endl;

        std::cout << "Enter choice: ";
        std::cin >> choice;

        if (choice == 4) {
            break;
        }

        std::cout << "Option selected";

    }

    std::cout << "ATM session ended";

    return 0;
}

29. Best Practices for break

  • Use break when the loop should stop immediately.
  • Keep the break condition easy to understand.
  • Use break for search operations when the target is found.
  • Use break in switch cases to prevent unwanted fall-through.
  • Remember that break affects the nearest enclosing loop or switch.
  • Avoid unnecessary break statements that make code difficult to follow.
  • Use a clear loop condition whenever possible.

30. break Statement – Final Summary

Concept Meaning
break; Immediately terminates the nearest enclosing loop or switch.
for Loop Can stop the loop before all iterations are completed.
while Loop Can terminate a repeating loop based on a condition.
do-while Loop Can stop the loop before another iteration.
switch Usually prevents execution from continuing into the next case.
Nested Loop break affects only the nearest enclosing loop.
Search Can stop searching once a required item is found.
if (condition) {
    break;
}

📌 Key Points

  • The break statement immediately terminates a loop or switch.
  • It can be used with for, while, and do-while loops.
  • It is also commonly used in switch statements.
  • Break usually appears inside an if statement.
  • After break executes, control moves outside the current loop or switch.
  • In nested loops, break terminates only the nearest enclosing loop.
  • Break is useful for searching arrays and strings.
  • Break can be used to exit menu-driven programs.
  • Break is different from continue, which skips only the current iteration.
  • Break is different from return, which exits the current function.

🧠 Quick Quiz

Question: What happens when a break statement is executed inside a loop?