if...else if...else statement
The if statement can be followed by the elseif…else statement, which can detect many possible situations.
When using if, else if, else statements, you need to pay attention to the following points:
If statement has at most 1 else statement, the else statement is after all elseif statements.
An If statement can have several elseif statements, which must be preceded by the else statement.
Once one of the else if statements detects true, the other else if and else statements will skip execution.
grammar
If...else syntax format is as follows:
if(boolean expression 1){
//Execute the code if the value of Boolean expression 1 is true
}else if(boolean expression 2){
//Execute the code if the value of Boolean expression 2 is true
}else if(boolean expression 3){
//Execute the code if the value of Boolean expression 3 is true
}else {
//If the above boolean expressions are not true, execute the code
}
Example
public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out. print("This is else statement"); } }}
The above code compilation and running results are as follows:
Value of X is 30
Nested if…else statement
Using nested if-else statements is legal. That is to say, you can use if or elseif statement in another if or elseif statement.
The syntax nested if...else syntax format is as follows:
if(boolean expression 1){
//// Execute the code if the value of Boolean expression 1 is true
if(boolean expression 2){
//// Execute the code if the value of Boolean expression 2 is true
}
}
You can nest else like if...else.
Example
public class Test { public static void main(String args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } }}
The above code compilation and running results are as follows:
X = 30 and Y = 10
Composite if- else if else statement example:
Convert percentage to grade
public class IfElseDemo06{ public static void main(String[] args){ int a=85; //Declare the int type variable a and assign a value of 85 if (a>90){ //Condition judgment System.out.println("Score" +a+" is A level"); }else if (a>80){ //Condition judgment System.out.println("Score"+a+" is B level"); }else if (a>70) { // Conditional judgment System.out.println("Score"+a+", is C-level"); }else if (a>60){ // Conditional judgment System.out.println("Score"+a+", is D level"); }else{ System.out.println("Score"+a+", is B level"); } }}
The operation results are shown in the figure.
A grade of 85 is B
Let's summarize the rules of the if-else statement:
1) The brackets after if cannot be omitted. The value of the expression in the brackets must ultimately return a Boolean value.
2) If there is only one statement in the condition body that needs to be executed, then the braces after if can be omitted, but this is an extremely bad programming habit.
3) For a given if, the else statement is optional, and the else if statement is also optional
4) When else and else appear at the same time, else must appear after else if
5) If multiple else if statements appear at the same time, if an expression test of an else if statement is successful, then all other else if and else branches will be ignored.
6) If multiple ifs and only one else occurs, the else clause belongs to the innermost if statement