Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      break statement

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      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
      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
      Special member functions
      Templates
      Miscellaneous
       
       

      Causes the enclosingfor,range-for,while ordo-while loop orswitch 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 ofattributes

      [edit]Explanation

      Appears only within thestatement of a loop body (while,do-while,for) or within thestatement of aswitch.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 thecondition 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. Thegoto statement may be used for this purpose.

      [edit]Keywords

      break

      [edit]Example

      Run this code
      #include <iostream> int main(){int i=2;switch(i){case1:std::cout<<"1";// <---- maybe warning: fall throughcase2:std::cout<<"2";// execution starts at this case label (+warning)case3:std::cout<<"3";// <---- maybe warning: fall throughcase4:// <---- maybe warning: fall throughcase5:std::cout<<"45";//break;// execution of subsequent statements is terminatedcase6: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:

      2345a0 a1 b0 b1

      [edit]See also

      (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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/break&oldid=174301"

      [8]ページ先頭

      ©2009-2025 Movatter.jp