Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      break statement

      From cppreference.com
      <c‎ |language
       
       
       
      Statements
      Labels
      Expression statements
      Compound statements
      Selection statements
      Iteration statements
      Jump statements
      break
       

      Causes the enclosingfor,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-spec-seq (optional)break;
      attr-spec-seq -(C23) optional list ofattributes, applied to thebreak statement

      Appears only within thestatement of a loop body (while,do-while,for) or within thestatement of aswitch.

      [edit]Explanation

      After this statement the control is transferred to the statement or declaration immediately following the enclosing loop or switch, as if bygoto.

      [edit]Keywords

      break

      [edit]Notes

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

      [edit]Example

      Run this code
      #include <stdio.h> int main(void){int i=2;switch(i){case1:printf("1");case2:printf("2");// i==2, so execution starts at this case labelcase3:printf("3");case4:case5:printf("45");break;// execution of subsequent cases is terminatedcase6:printf("6");}printf("\n"); // Compare outputs from these two nested for loops.for(int j=0; j<2; j++)for(int k=0; k<5; k++)printf("%d%d ", j,k);printf("\n"); for(int j=0; j<2; j++){for(int k=0; k<5; k++)// only this loop is exited by break{if(k==2)break;printf("%d%d ", j,k);}}}

      Possible output:

      234500 01 02 03 04 10 11 12 13 1400 01 10 11

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.8.6.3 The break statement (p: 111)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.8.6.3 The break statement (p: 153)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.8.6.3 The break statement (p: 138)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.6.6.3 The break statement

      [edit]See also

      (C23)
      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 forbreak statement
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/break&oldid=155195"

      [8]ページ先頭

      ©2009-2025 Movatter.jp