Java foundation chapter IV (Select structured titanium)

A, switch selection structure

1、switch

grammar

switch (expression) {
  Case 1 Constant:
    // code block 1
  BREAK;
  Case 2 constants:
    // Code Block 2
  BREAK;
  default:
    // code block n-
  BREAK;
}

Second, the structure selected summary:

1、if:

if (condition) {
Code // conditions established code block to be executed
}

2, if-else selection structure

if (condition) {
code block
} else {
code block
]

3, multi-if selection structure

if (condition) {
Code
} else if (condition 2) {
Code 2
} the else {
Code 3
}

4, if a nested selection structure

if (condition. 1) {
if (condition 2) {
Code. 1
} the else {
Code 2
}
} the else {
Code 3
}

5、switch

 

This chapter concludes:

Determining if replaced with equivalent Switch, code clearer, do not forget to break;

In order to increase the robustness of the program and to be active in increasing Analyzing program prompts the user good

Often used in the actual development if and switch integrated to solve the problem

Guess you like

Origin www.cnblogs.com/aryl/p/12141628.html