Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/break"

From cppreference.com
< cpp‎ | language
m (navbar try)
m (Consistency with cpp/language/alignas)
 
(14 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{title|break statement}}
+
{{title|{{tt|break}} statement}}
 
{{cpp/language/statements/navbar}}
 
{{cpp/language/statements/navbar}}
Causes the enclosing {{rlp|for}}, {{rlp|while}} or {{rlp|do|do-while}} loop or {{rlp|switch| switch statement}} to terminate.
+
Causes the enclosing {{rlp|for}}, {{rlp|range-for}}, {{rlp|while}} or {{rlp|do|do-while}} loop or {{rlp|switch|switch statement}} to terminate.
  
 
Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.
 
Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.
  
 
===Syntax===
 
===Syntax===
 +
{{sdsc begin}}
 +
{{sdsc|{{spar optional|attr}} {{ttb|break}} {{ttb|;}}}}
 +
{{sdsc end}}
  
{{sdcl list begin}}
+
{{par begin}}
{{sdcl list item | {{sparam|attr}}{{mark optional}} {{ttb|break}} {{ttb|;}} }}
+
{{par|{{spar|attr}}|{{mark since c++11}} any number of {{rlp|attributes}}}}
{{sdcl list end}}
+
{{par end}}
  
 
===Explanation===
 
===Explanation===
 +
Appears only within the {{spar|statement}} of a loop body ({{rlpt|while}}, {{rlpt|do|do-while}}, {{rlpt|for}}) or within the {{spar|statement}} of a {{rlpt|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 {{spar|condition}} of a loop/switch are destroyed, in reverse order of construction, before the execution of the first line following the enclosing loop.
  
After this statement the control is transferred to the statement immediately following the enclosing loop. All automatic storage objects declared in enclosing loop are destroyed 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 {{rlp|goto|goto statement}} may be used for this purpose.
  
 
===Keywords===
 
===Keywords===
 
+
{{ltt|cpp/keyword/break}}
{{ltt|cpp/keywords/break}}
+
  
 
===Example===
 
===Example===
{{example  
+
{{example
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
  
Line 27: Line 32:
 
{
 
{
 
     int i = 2;
 
     int i = 2;
     switch (i) {
+
     switch (i)
         case 1: std::cout << "1";
+
    {
         case 2: std::cout << "2";  //execution starts at this case label
+
         case 1: std::cout << "1";   // <---- maybe warning: fall through
         case 3: std::cout << "3";
+
         case 2: std::cout << "2";  // execution starts at this case label (+warning)
         case 4:
+
         case 3: std::cout << "3";   // <---- maybe warning: fall through
         case 5: std::cout << "45";
+
         case 4:                     // <---- maybe warning: fall through
                 break;              //execution of subsequent statements is terminated
+
         case 5: std::cout << "45"; //
 +
                 break;              // execution of subsequent statements is terminated
 
         case 6: std::cout << "6";
 
         case 6: std::cout << "6";
 
     }
 
     }
 
 
     std::cout << '\n';
 
     std::cout << '\n';
+
 
     for (int j = 0; j < 2; j++) {
+
     for (char c = 'a'; c < 'c'; c++)
         for (int k = 0; k < 5; k++) {        //only this loop is affected by break
+
    {
             if (k == 2) break;
+
         for (int i = 0; i < 5; i++)     // only this loop is affected by break
             std::cout << j << k << " ";
+
        {                                //
 +
             if (i == 2)                 //
 +
                break;                   //
 +
             std::cout << c << i << ' '; //
 
         }
 
         }
 
     }
 
     }
 +
    std::cout << '\n';
 
}
 
}
| output=
+
|p=true
 +
|output=
 
2345
 
2345
00 01 10 11
+
a0 a1 b0 b1
 
}}
 
}}
  
[[de:cpp/language/break]]
+
===See also===
[[es:cpp/language/break]]
+
{{dsc begin}}
[[fr:cpp/language/break]]
+
{{dsc inc|cpp/language/attributes/dsc fallthrough}}
[[it:cpp/language/break]]
+
{{dsc see c|c/language/break}}
[[ja:cpp/language/break]]
+
{{dsc end}}
[[pt:cpp/language/break]]
+
 
[[ru:cpp/language/break]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[zh:cpp/language/break]]
+

Latest revision as of 11:16, 11 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
continue - break
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

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.

Contents

[edit] Syntax

attr (optional) break ;
attr - (since C++11) any number of attributes

[edit] 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.

[edit] Notes

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

[edit] Keywords

break

[edit] Example

#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:

2345
a0 a1 b0 b1

[edit] See also

[[fallthrough]](C++17) 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)[edit]
C documentation for break