C program flow control switch statement

switch statement structure

switch(表达式)
{
   case 判断值1;
           语句组1;
            break;
   case 判断值2;
           语句组2;
            break;
   case 判断值3;
          语句组3;
           break;
    ……
   case 判断值n;
           语句组n;
            break;
   default:
           语句组n+1;
           break;
}

The expression is a selection condition, which can be a single variable or a combined expression. The final result must be an integer value. All the content in {} is the main body of the switch statement and contains multiple case branches. The judgment value must be Is a constant, the case branch identifies the entry selected by the condition according to the judgment value; the break statement is used to exit the switch statement, if the break statement is not used, the program will be executed sequentially.

Note:
1. The expression in parentheses after switch() requires the result to be an integer (integer variable), and the judgment value of each case requires an integer constant.
2. The order of each case and default and the statement group below is arbitrary, but the judgment value behind each case must be a different value.
3. The break statements of multiple branch statement groups play the role of exiting the switch-case structure. If there is no such statement, the program will execute the next case statement group in sequence.
4. When the result value of the expression is inconsistent with the judgment value of all case, the program executes the statement group of the default part. So the default part is not necessary.

Guess you like

Origin blog.csdn.net/weixin_43553142/article/details/112407441