Switch is used in programming. For example, it is often used together with case in C language.Code. Its function is to control the flow of business processes.
The syntax of the switch statement is as follows (switch, case, and default are keywords ):
Switch (controllingexpression)
{
Case constantexpression1:
Case constantexpression2:
Case constantexpression2:
Statements; // when constantexpression1, constantexpression2, and constantexpression3 are met, statements is executed.
Break;
Case constantexpression:
Statements;
Break;
...
Default:
Statements;
Break;
}
Observe switch statement rules
The switch statement is very useful, but you must be cautious when using it. Any switch statement must follow the following rules:
Only switch can be used for basic data types, including int and char. For other types, the IF statement must be used.
The case tag must be a constant expression (constantexpression), such as 42 or "42 ". If you need to calculate the value of the Case tag at runtime, you must use the if statement.
The case tag must be a unique expression. That is to say, two cases cannot have the same value.