Java selection structure (if statement switch statement)
1.IF Selection Structure:
①if (Boolean expression) {
If the Boolean expression is true, the statement executed
}
* If there is only one statement after the IF statement, the curly brace of the IF statement may not
②if (Boolean expression) {
If the Boolean expression is true, execute the
}else{
Executes if the Boolean expression is False
}
* An else can only follow a related If,else statement cannot appear alone
③if (Boolean expression) {
If the Boolean expression is true, execute the
}else if (Boolean expression) {
Executes if the Boolean expression is False
}else if (Boolean expression) {
Executes if the Boolean expression is False
. . . . . .
}else{
If all of the above are false, execute the
}
* Above a if/else block is executed, the control flow will then jump out of the if/else structure,
* and subsequent Boolean expressions are not detected
Case:
2..SWITCH selection Structure
Switch (variable) {
Case value:--->case followed by a colon, the data type of the value is the same as the variable, must be constant or literal
Statement
break;----> can not write, if not write, will continue to execute the next case;
Case Value:
Statement
break;----> can not write, if not write, will continue to execute the next case;
Case Value:
Can have a lot of case statements;
Break,----> If written, will jump out of the switch statement directly to the next line of the switch statement
Default:---> can write not write
Statement----> All case does not match, it executes
}
The *switch statement allows a variable to be tested for equality with multiple values, not a larger, smaller comparison, each value being
* A case statement invocation and detection.
Case:
Java selection Structure------------if statement switch statement