Java's Switch Case statements can be understood as a branch
example assumes that there are four people playing mahjong number 1234 corresponds to the name John Doe, Wang Wu, Zhao six
data structure
Switch (expression The) { case value: // statements BREAK; // optional case value: // statements BREAK; // optional // you can have any number of case statement default: // optional // statements }
Sample Code
code = 2 int; Switch (code) { Case. 1: System.out.println ( "I am John Doe"); BREAK; Case 2: System.out.println ( "I am John Doe"); BREAK; Case. 3 : System.out.println ( "I am the king five"); BREAK; Case 4: System.out.println ( "I'm Zhao six"); BREAK; default: System.out.println ( "I'm just a spectator." ); }
Export
I'm John Doe
break that out, when a case is satisfied, stop below the other case.
The above process is executed
Q: code How much is the implementation of case 1 discovery does not satisfy the execution case 2 found met, exit switch immediately
If you do not write break
code = 2 int; Switch (code) { Case. 1: System.out.println ( "I am John Doe"); Case 2: System.out.println ( "I am John Doe"); Case. 3: the System.out .println ( "I am the king five"); Case 4: System.out.println ( "I'm Zhao six"); default: System.out.println ( "I'm just a spectator"); }
Export
I'm John Doe I was king five I was Zhao six I'm just a spectator
Procedure flow chart
Q: code How much is the implementation of case 1 discovery does not satisfy the execution case 2 was found to meet the output execution case 3 judges whether or not the output execution case 4 judges whether or not the output execution is judged whether or not default, output
If the current case does not break upon successful determination, subsequent case will be executed.
Important:
1. Switch control process, a bit like if else but the efficiency is determined in the case of multilayer if else higher than
2. Break critical
Case 3. Switch in value can not be repeated case 1: case next to continue 1: