break
statement
| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
|
||||||||||||||||
| Jump statements | ||||||||||||||||
|
||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline
specifier
|
||||||||||||||||
| Dynamic exception specifications ( until C++17* ) | ||||||||||||||||
noexcept
specifier
(C++11)
|
||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
|
||||||||||||||||
| 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 | ||||||||||||||||
|
||||||||||||||||
| Special member functions | ||||||||||||||||
|
||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
해당 for , range-for , while 또는 do-while 루프 또는 switch 문 을 종료시킵니다.
조건식과 조건문을 사용하여 루프를 종료하는 것이 어색한 경우 사용됩니다.
목차 |
구문
attr
(선택 사항)
break
;
|
|||||||||
| attr | - | (since C++11) 임의 개수의 attributes |
설명
루프 본문(
while
,
do-while
,
for
)의
statement
내부 또는
switch
의
statement
내부에서만 나타납니다.
이 문장 이후 제어는 해당 루프나 switch를 감싸는 문장 바로 다음으로 이전됩니다. 모든 블록 종료와 마찬가지로, 감싸는 복합 문장이나 루프/switch의
condition
에 선언된 모든 자동 저장 객체들은 생성 순서의 역순으로 파괴된 후, 감싸는 루프 다음의 첫 번째 줄이 실행됩니다.
참고 사항
break 문은 여러 개의 중첩된 루프를 벗어나는 데 사용할 수 없습니다. 이러한 목적으로는 goto 문 을 사용할 수 있습니다.
키워드
예제
#include <iostream> int main() { int i = 2; switch (i) { case 1: std::cout << "1"; // <---- 경고 발생 가능: fall through case 2: std::cout << "2"; // 이 case 레이블에서 실행 시작 (+경고) case 3: std::cout << "3"; // <---- 경고 발생 가능: fall through case 4: // <---- 경고 발생 가능: fall through case 5: std::cout << "45"; // break; // 이후 문장들의 실행이 종료됨 case 6: std::cout << "6"; } std::cout << '\n'; for (char c = 'a'; c < 'c'; c++) { for (int i = 0; i < 5; i++) // 이 루프만 break의 영향을 받음 { // if (i == 2) // break; // std::cout << c << i << ' '; // } } std::cout << '\n'; }
가능한 출력:
2345 a0 a1 b0 b1
참고 항목
[[
fallthrough
]]
(C++17)
|
이전 case 레이블에서의 폴스루(fallthrough)가 의도적이며 폴스루에 대해 경고하는 컴파일러에 의해 진단되지 않아야 함을 나타냅니다
(속성 지정자) |
|
C 문서
for
break
|
|