Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - The meaning and usage of case in c language
The meaning and usage of case in c language
In C language, case and switch together form a switch—case statement, which is used to judge and select, and case is used to express the selection structure.

The general form of the switch statement is:

Switch (emoticon)?

Case constant expression 1:? Statement1;

Case constant expression 2:? Statement 2;

…?

Case constant expression n:? Statement n;

Default:? Statement n+1; }

Its semantics are: calculating the value of an expression. And compare the values of the following constant expressions one by one. When the value of the expression is equal to the value of a constant expression, execute the following statement, then stop judging and continue to execute all statements after the case. If the value of the expression is different from all constant expressions after case, the statement after default will be executed.

Extended data

Any switch statement must follow the following rules:

The 1. switch can only be used for integer types in basic data types, including int and char. For other types, if statements must be used.

2. The parameter type of 2.switch () cannot be a real number.

3. The 3.case tag must be a constant expression, such as 42 or "42".

4. The 4.case tag must be a unique expression; That is, two case are not allowed to have the same value.

Baidu encyclopedia-switch