Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Example of switch box usage
Example of switch box usage
The general form of the Switch case statement is as follows.

When using the switch box, be careful not to ignore the disconnection. If there is no interruption, the program will execute the following statements in turn after executing the corresponding code block until the end of the program or the next interruption. When the value of the expression is equal to which of the following constants, the code block corresponding to the constant will be executed. If there is no constant equal to the expression, the code block below default will be executed.

When multiple constants correspond to the same code block, we can also use switch case in this way.

If you write this way, whether you enter 0 or 1, the result is the code block before line break. When multiple situations correspond to the same output, case with the same code block can be put together directly, which can effectively reduce duplicate code, save time and improve development efficiency.

Constant values in all case clauses of the same Switch statement are different from each other.

When is it better to use switch case?

If there are not many specific numerical values and meet several types, such as byte, short, char, int, String, enumeration, etc. It is recommended to use the swtich statement.

code fo practice

The' expression in parentheses after 1.swich must be of integer type. In other words, it can be an int variable, a char variable, an integer, or a character constant directly. This number is negative, but it must not be a real number. Float variables, double variables and decimal constants are all bad, which are all grammatical errors.

Case and defaut under switich must be enclosed in a pair of braces.

You only want to execute this case statement and don't want to execute other cases, so you need to add break after this case statement and jump out of the swich statement.

3) When the value of "expression" in parentheses after switch is equal to the value of "constant expression" after case, execute the statement after case. After executing the statement after one case, flow control will be transferred to the next case. If you only want to execute this case statement and don't want to execute other cases, you need to add break after this case statement and jump out of the switch statement.

Similarly, switch is a "select" statement, not a "loop" statement. Many novices think that break is a loop statement when they see it, because break generally gives us the impression of jumping out of the "loop", but break has another usage, that is, jumping out of the switch.

4) If the value of the constant expression is not equal to the value of "expression" in parentheses after switch in all cases, the statement after defaul is executed, that is, "default". If slander is the last word, then it

After that, you can not add break, because since it is the last sentence, you will naturally exit the switch after execution.

5) The values of the "constant expression" after each box must be different from each other, otherwise there will be contradictions, and such writing will cause grammatical errors.

6) "Case constant expression only plays the role of statement label, and is not judged there. When executing the switch statement, find the matching entry label according to the value of the expression after switch, and then start execution from this label without judging the solution.

7) The order in which each case and default appear does not affect the execution result, but from the reading point of view, it is best to write them in alphabetical or numerical order.