1. Simple if statement
If, else belongs to the conditional branch
if (condition) {
EXECUTE statement
}else{
EXECUTE statement
}
Points:
Program Flow
Sequential execution
Select Execute
Loop execution
Conditional branching statements are used to select the execution
2. Multiple if, Else statements
Grammar
if (condition 1) {
This block of code is executed when the condition is met 1 o'clock
}else if (condition 2) {
This block of code is executed when the condition is met 2 o'clock
}else if (condition 3) {
This block of code is executed when the condition is met 3 o'clock
}else{
Performed when the above 3 conditions are not met
}
Important: Else always belongs to the most recent if that does not correspond to else
3.switch, Case statement
Grammar
Switch (value) {
CASE constant 1:
Statement 1;
Break
Case constant 2:
Statement 2;
Break
Case Constant 3:
Statement 3;
Break
Default
Statement 3;
}
Points:
Value is not a long, can be integer, character type
Break statements must be added to prevent leaks;
default is optional;
The result of an expression is a fixed value instead of a range
Java Fundamentals (3) Conditional statements