C switch statement

C Decision Making

The switch statement is a restricted control flow statement that is used to execute different code blocks based on the value of an expression.

aswitchThe statement allows testing a variable against multiple values, each value is called a case, and the variable being tested will be compared with eachswitch casePerform a check.

Syntax

In C languageswitchStatement syntax:

switch(expression){ case constant-expression : statement(s); break; /*Optional*/ case constant-expression : statement(s); break; /*Optional*/ /*You can have any number of case statements*/ default : /*Optional*/ statement(s); }

Description of the switch statement:

  • The value of the expression after switch will be compared with the constant value after each case until a matching value is found or execution reaches default (if it exists).
  • If a matching value is found, the code block after the corresponding case is executed, and then the switch statement is exited.
  • If there is no matching value and default exists, the code block after default is executed.
  • If there is no matching value and no default, the entire switch statement is skipped until the end.

switchThe statement must follow the rules below:

  • Type of the switch expression:The expression in a switch statement must be of an integer type (char, short, int, or enum), or an expression that can be implicitly converted to an integer type.

  • Uniqueness of the case labels:In a switch statement, each case label must be unique; duplicate values are not allowed.

  • Optionality of the default case:The default label in a switch statement is optional. If no case label matches, the code block under the default label (if it exists) will be executed.

  • Constant values in the case labels:The value after a case label must be a constant expression, meaning its value can be determined at compile time.

  • Order of the case labels:The order of case labels in a switch statement is not important; they can be written in any order. The program matches them in the order in which the case labels appear.

  • Use of the break statement:At the end of the code block for each case label, a break statement is usually needed to terminate the execution of the switch statement. If there is no break statement, the program will continue to execute the code in the next case label until it encounters a break statement or the end of the switch statement.

  • Nesting of switch statements:switch statements can be nested within other switch statements, but attention should be paid to code readability and complexity.

  • The range of case labels and expressions:The case labels in a switch statement can be integer constant expressions, but cannot be floating-point numbers or strings.

Flowchart

C 中的 switch 语句

Example

#include <stdio.h> int main () { /*Local variable definition*/ char grade = 'B'; switch(grade) { case 'A' : printf("Great!\n" ); break; case 'B' : case 'C' : printf("Well done\n" ); break; case 'D' : printf("You passed\n" ); break; case 'F' : printf("Better try again\n" ); break; default : printf("Invalid score\n" ); } printf("Your grade is %c\n", grade ); return 0; }

When the above code is compiled and executed, it produces the following result:

做得好
您的成绩是 B

C Decision Making

other extensions