Understand Java in the switch branch statement

Preface:

Encounter multi-branch selected in the program when surely we all like to use if ... else if ... else ... statement, especially for beginners, because until we know the switch statement, I also only use if. ..else statement. So now after reading this essay's introduction, you have another option, use the switch statement can enhance the possibility of code, of course switch is not casually be able to use, it also constrained grammar rule, look at the following elaboration.

A, switch profiles and grammar

Multi-branch selection for the switch statement, the control by the expression and a plurality of case tags. Controlling expression of a switch statement behind the data type can only be a byte, short, char, int, String and enumerated types, not other types.

often you need to keep a switch statement block, case as the tag identification code block after the case label.

switch statement has the following syntax:

Switch (expr) {
 Case condition1 {
 // code block ...
 BREAK;
 }
 Case condition2 The {
 // code block ...
 BREAK;
 }
 ...
 Case conditionN {
 // code block ...
 BREAK;
 }
 dafault: {
 ... // code block
 }
}

This statement is executed to branch to expr evaluated, followed by the matching condition of each case label value, i.e., the matching value is encountered executes a corresponding code block, if the condition value is not equal to the value of the expression expr, code block is executed after the default label.

Second, the difference between the switch statement and if statements

switch statement and if statements can be used to select the branch condition, but the control expression behind the switch can only be byte, short, char, int, String and enumerated types, can not be a boolean type, controlling expression following the if is boolean type; Switch statement after each case label code block start point and an end point very clear, and therefore can code block after braces case will be omitted.

Three, switch use and precautions

For a more clear understanding of the switch, the switch below to demonstrate the use of a program by:

Running the above program output: Java language.

Because the definition of variables (behind the switch control expression) "languageType" and "Java" match.

If the above procedure in each case label in the break statement removed, what kind of results will happen? So I put every break statement commented, results are as follows:


Java language C ++ language Python Language type of input errors

The output is not very strange! This is a switch statement the decision to run the process, as long as the entrance into the switch statement, the program will run forever, until it encounters a break statement, if you omit the break statement, then all the situation after the matching case value (including default) will be carried out.

So, use a switch statement, there are two places worth noting:

1) expression behind the control switch can only be byte, short, char, int, String and enumerated types;

2) If the code blocks is omitted break the case, we will introduce a trap.

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160772.htm