# break statement

> C++ Atlas: `cppatlas.com/w/cpp/language/break` | Source: https://en.cppreference.com/w/cpp/language/break | Domain: Language | Standards: C++11, C++17

Causes the enclosing for, range-for, while or do-while loop or switch statement to terminate.

## Official Specification Summary (cppreference)

Causes the enclosing for, range-for, while or do-while loop or switch statement to terminate.

Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.

### Syntax

- *attr* (optional) `break` `;`

- `attr` - (since C++11) any number of attributes

### Explanation

Appears only within the *statement* of a loop body (`while`, `do-while`, `for`) or within the *statement* of a `switch`. After this statement the control is transferred to the statement immediately following the enclosing loop or switch. As with any block exit, all automatic storage objects declared in enclosing compound statement or in the *condition* of a loop/switch are destroyed, in reverse order of construction, before the execution of the first line following the enclosing loop.

### Notes

A break statement cannot be used to break out of multiple nested loops. The goto statement may be used for this purpose.

### Keywords

`break`

### Example

```cpp
#include <iostream>

int main()
{
    int i = 2;
    switch (i)
    {
        case 1: std::cout << "1";   // <---- maybe warning: fall through
        case 2: std::cout << "2";   // execution starts at this case label (+warning)
        case 3: std::cout << "3";   // <---- maybe warning: fall through
        case 4:                     // <---- maybe warning: fall through
        case 5: std::cout << "45";  //
                break;              // execution of subsequent statements is terminated
        case 6: std::cout << "6";
    }
    std::cout << '\n';

    for (char c = 'a'; c < 'c'; c++)
    {
        for (int i = 0; i < 5; i++)      // only this loop is affected by break
        {                                //
            if (i == 2)                  //
                break;                   //
            std::cout << c << i << ' ';  //
        }
    }
    std::cout << '\n';
}
```

Possible output:

```text
2345
a0 a1 b0 b1
```

### See also

- [`[[fallthrough]]` (C++17)](https://en.cppreference.com/w/cpp/language/attributes/fallthrough): indicates that the fall through from the previous case label is intentional and should not be diagnosed by a compiler that warns on fall-through (attribute specifier)
- [C documentation for `break`](https://en.cppreference.com/w/c/language/break)

---
Attribution: content from https://en.cppreference.com/w/cpp/language/break (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.