Learn to program the Java Learning switch selection structure released, welcome to visit through Xuebiancheng8.com
The previous analysis of Java's Process control is divided into the choice structure, the loop structure, wherein the choice structure can be divided into the if choice structure and the switch structure. Let's analyze the switch...case structure.
The switch...case structure is also used to implement the selection structure
The syntax is as follows:
Switch (variable) {
CASE constant 1:
......
Break
Case constant 2:
......
Break
Default
......
Break
}
Syntax is too abstract, next look at the code
int a = 10;
Switch (a) {
Case 5:
System.out.println ("A has a value of 5");
Break
Case 10:
System.out.println ("A has a value of 10");
Break
Default
System.out.println (the value of "a" is neither 5 nor 10 ");
Break
}
In the above example, to determine the value of a, when the value of a is 5 o'clock, execute the code of Case 5, when the value of a is 10 o'clock, the code of the execution case 10, when a is not 5 is not 10, the code that executes the default
What is the difference between if and switch...case, if used with the condition is range, say a>10, this representation range. And Switch...case is used when the condition is fixed value.
Of course, there are a lot of things to be aware of switch...case, such as when necessary, then the case must be added after the Break;switch parentheses can be Byte,short,int,string,char, enumeration of these types, Nothing else can be put in brackets.
Please come and visit your classmates xuebiancheng8.com.
Http://xuebiancheng8.com/play/goodgoodstudy_54_daydayup.html