On Java in the switch branch statement

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: { 
        // 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:

public  class TestSwitch { 

    public  static  void main (String [] args) {
         // declare variables languageType 
        String languageType = "the Java" ;
         // execute switch statement 
        switch (languageType) {
         Case "C" : 
            System.out.println ( "C language " );
             BREAK ;
         Case " the Java " : 
            System.out.println ( " the Java language " );
             BREAK ;    
         Case " C ++ " : 
            System.out.println ( "C ++ Language");
             BREAK ;
         Case "the Python" : 
            System.out.println ( "the Python language" );
             BREAK ;
         default : 
            System.out.println ( "Error Input language type" ); 
        } 

    } 

}

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 
language type input error

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.

 

Focus on micro-channel public number] [Java books, Java technology watch more dry! Java concern Jisong a full set of data

   ▼ sweep the next micro-channel two-dimensional code Follow FIG ↓↓↓

 

 

Guess you like

Origin www.cnblogs.com/bingyimeiling/p/11544720.html