Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      switch statement

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

      Executes code according to the value of an integral argument.

      Used where one or several out of many branches of code need to be executed according to an integral value.

      Contents

      [edit]Syntax

      attr-spec-seq(optional)switch (expression)statement
      attr-spec-seq -(C23)optional list ofattributes, applied to theswitch statement
      expression - anyexpression ofinteger type (char, signed or unsigned integer, or enumeration)
      statement - anystatement (typically a compound statement).case: anddefault: labels are permitted instatement, andbreak; statement has special meaning.
      caseconstant-expression:statement (1)(until C23)
      attr-spec-seq(optional)caseconstant-expression:statement(optional) (1)(since C23)
      default:statement (2)(until C23)
      attr-spec-seq(optional)default:statement(optional) (2)(since C23)
      constant-expression - any integerconstant expression
      attr-spec-seq -(C23)optional list ofattributes, applied to the label

      [edit]Explanation

      The body of a switch statement may have an arbitrary number ofcase: labels, as long as the values of allconstant-expressions are unique (afterconversion to thepromoted type ofexpression). At most onedefault: label may be present (although nested switch statements may use their owndefault: labels or havecase: labels whose constants are identical to the ones used in the enclosing switch).

      Ifexpression evaluates to the value that is equal to the value of one ofconstant-expressions after conversion to the promoted type ofexpression, then control is transferred to the statement that is labeled with thatconstant-expression.

      Ifexpression evaluates to a value that doesn't match any of thecase: labels, and thedefault: label is present, control is transferred to the statement labeled with thedefault: label.

      Ifexpression evaluates to a value that doesn't match any of thecase: labels, and thedefault: label is not present, none of the switch body is executed.

      Thebreak statement, when encountered anywhere instatement, exits the switch statement:

      switch(1){case1:puts("1");// prints "1",case2:puts("2");// then prints "2" ("fall-through")}
      switch(1){case1:puts("1");// prints "1"break;// and exits the switchcase2:puts("2");break;}

      As with all other selection and iteration statements, the switch statement establishesblock scope: any identifier introduced in theexpression goes out of scope after thestatement.

      If a VLA or another identifier with variably-modified type has acase: or adefault: label within its scope, the entire switch statement must be in its scope (in other words, a VLA must be declared either before the entire switch or after the last label):

      switch(expr){int i=4;// not a VLA; OK to declare here        f(i);// never called//      int a[i]; // error: VLA cannot be declared herecase0:        i=17;default:int a[i];// OK to declare VLA hereprintf("%d\n", i);// prints 17 if expr == 0, prints indeterminate value otherwise}
      (since C99)

      [edit]Keywords

      switch,case,default

      [edit]Example

      Run this code
      #include <stdio.h> void func(int x){printf("func(%d): ", x);switch(x){case1:printf("case 1, ");case2:printf("case 2, ");case3:printf("case 3.\n");break;case4:printf("case 4, ");case5:case6:printf("case 5 or case 6, ");default:printf("default.\n");}} int main(void){for(int i=1; i<9;++i) func(i);}

      Output:

      func(1): case 1, case 2, case 3.func(2): case 2, case 3.func(3): case 3.func(4): case 4, case 5 or case 6, default.func(5): case 5 or case 6, default.func(6): case 5 or case 6, default.func(7): default.func(8): default.

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.8.4.2 The switch statement (p: 108-109)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.8.4.2 The switch statement (p: 149-150)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.8.4.2 The switch statement (p: 134-135)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.6.4.2 The switch statement

      [edit]See also

      C++ documentation forswitch statement
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/switch&oldid=138532"

      [8]ページ先頭

      ©2009-2025 Movatter.jp