Switch Branch Structure
I,Program running processCategory
1. Ordered Structure: Execute in sequence according to program order.
2. Branch Structure: Determine the execution sequence of the program based on the judgment.
3. loop structure.
Three elements of the branch structure: (1) there must be a clear judgment statement.
(2) The statement to be executed when the condition is true.
(3) statements to be executed when the condition is false.
Ii. Detailed discussion of the Branch Structure
1. The if... else... structure has three conditions.
(1) syntax structure:
If (Boolean expression ){
Statement 1;
Statement 2;
......
}
(2) syntax structure:
If (Boolean expression ){
Statement 1;
Statement 2;
......
}
Else {
Statement 1;
Statement 2;
......
}
(3) Statement Structure
If (Boolean expression ){
Statement 1;
Statement 2;
......
}
Else if (Boolean expression ){
Statement 1;
Statement 2;
......
}
Switch
Switch (expression ){//The result of the expression is first compared with constant 1. If it is the same, it is output. If it is other expressions, It is not judged. Break is paused,Constant 1 is different. It is compared with 2nd ,''''
Case constant 1: Statement 1; break;
Case constant 2: Statement 2; break;
......
Case constant N: Statement N; break;
[Default: Default statement]; optional if not written
}
Note: The expression in the switch can only be of four types
1. byte 2, short 3, int 4, char
Case constant 1: Statement 1; break;
Case constant 2: Statement 2; break;
......
Case constant N: Statement N; break;
Default: the default statement;
}
Note: The expression in the switch can only be of four types
1. byte 2, short 3, int 4, char
Public class testswitch {<br/> Public static void main (string ARGs []) {<br/> char c = 'a'; <br/> switch (c) {<br/> case 'B': <br/> system. out. println ('B'); <br/> break; <br/> case 'C': <br/> system. out. println ('C'); <br/> break; <br/> case 'A': <br/> system. out. println ('A'); <br/> break; <br/> default: <br/> system. out. println ('D'); </P> <p >}< br/>}Output:
In another case, look at the key code
Char c = 'a'; <br/> switch (c) {<br/> case 'B': <br/> system. out. println ('B'); <br/> break; <br/> case 'A': <br/> system. out. println ('A'); <br/> case 'C': <br/> system. out. println ('C'); <br/> break; <br/> default: <br/> system. out. println ('D ');
Output is a C
Case 'B': <br/> system. out. println ('B'); <br/> break; <br/> case 'C': <br/> system. out. println ('C'); <br/> break; <br/> case 'A': <br/> system. out. println ('A'); <br/> default: <br/> system. out. println ('D ');Output:
A
D