Java if, else-if Statement
Most of the time, you want to execute a block of code only under certain conditions. The if statement is one of the conditional statements to execute a particular block of code if and only if a boolean expression evaluates to true. This tutorial will teach you about the usage of if and else-if statements in the Java language.
The if Statement in Java Language
The if statement is one of the simple conditional statements. The syntax of if statement is:
if (expression)
statement;
- The expression is any valid Java expression, also known as conditional expression.
- The expression evaluates to either true or false.
- The statement is only executed if the result of the expression is true.
if ( 80 > 50 )
System.out.println("Condition is true, 80 is greater than 50");
// Output: Condition is true, 80 is greater than 50
The expression 80 > 50 is evaluated and the message is only displayed when the result of the expression is true. As 80 is greater than 50, the statement is executed and message is displayed.
int x = 1;
if(x==1)
System.out.println("Condition is true");
// Output: Condition is true
Here, x is equals to 1, expression is evaluated to true, the statement is executed. Let’s try above code in another way:
int x = 1;
if(x!=0)
System.out.println("X is not zero");
// Output: X is not zero
The value of x in above example is not equals to zero, expression is evaluated to true, the statement is executed.
In the above example only one statement is executed, if the expression is evaluated to true. A code block with multiple statements may also be executed enclosed in curly braces { }.
if(expression)
{
statement 1;
statement 2;
statement 3;
}
Let’s try if condition with block of multiple statements.
int x = 1;
if(x==1) {
System.out.println("Condition is true");
System.out.println("This is second line of code block");
}
In the above example, x is true. In this case, the block of code enclosed in braces { } is executed.
The if else Statement in Java Language
In the above section, you learned about the if statement, which allows the execution of a statement or block of statements when an expression is true. But what if you need to execute a particular statement or a block of statements when the expression is false? This is where the if-else statement comes into play. Its syntax is as follows:
if(expression)
statement1;
else
statement2;
The if-else statement has following flow:
- If the expression is true, only the statement or block of statements following the if keyword is executed.
- If the expression is false, the control is transferred to statement or block of statements following the else keyword.
- else part is optional.
int marks = 80;
if (marks > 90)
// This statement is executed, if expression is true
System.out.println("You get A grade");
else
// This statement is executed, if expression is false
System.out.println("You didn't get A grade");
// Output: You didn't get A grade
In the above example, value of variable marks is 80. The expression is evaluated to false. The control is transferred to statement following the else keyword. In this case text “You didn’t get A grade” is printed.
The Nested if in Java Language
You can nest the if statements to any depth required. Nesting means one if statement is part of body of another if statement.
// Outer if condition
if (expression1)
// Inner if condition
if (expression2)
statement;
First expression1 is evaluated, if result is true, then expression2 is evaluated. The control will not be transferred to inner if condition if expression of outer if condition is evaluated to false. The statement will not be executed unless both expression1 and expression2 are true.
int x = 7;
if(x > 5)
if (x < 10)
System.out.println("Value of x is between 5 and 10");
\\ Output: Value of x is between 5 and 10
Here, value of x is 7. the expression x>5 of outer if condition is evaluated to true. The control is transferred to inner if condition. the expression x<10 of inner if condition is evaluated to true. The cout statement is executed.
See following example, when outer if evaluates to false.
int x = 3;
if(x > 5)
if (x < 10)
System.out.println("Value of x is between 5 and 10");
In the above example, value of x is 3. The expression x>5 of outer if is evaluated to false. In this case, the control is not transferred to inner if condition and statement is also not executed.
You can also use optional else statement in nested if. But the confusion is, else is associated with which if condition, outer if or inner if? The answer, else statement is associated to the nearest previous (else-less) if. See example below:
int x = 15;
if(x > 5)
if (x < 10)
System.out.println("Value of x is between 5 and 10");
else
System.out.println("Value of x is greater than 10");
Here, the value of x is 15. The expression of outer if evaluates to true, control is transferred to inner if. The expression of inner if evaluates to false, control is transferred to its associated else statement. The else is associated with if (x < 10 ), which is its nearest previous if.
You can use braces { } to associate else with outer if statement, as the following example shows:
int x = 1;
if(x > 5){
if (x < 10)
System.out.println("Value of x is between 5 and 10");
}
else
System.out.println("Value of x is less than 5");
// Output: Value of x is less than 5
Following example shows the else statements associated with their respective outer and inner if statements.
int x = 1;
if(x > 5)
if (x < 10)
System.out.println("Value of x is between 5 and 10");
else
System.out.println("Value of x is greater than 10");
else
System.out.println("Value of x is less than 5");
Here, the value of x is 1. The expression of outer if evaluates to false. The control is transferred to its associated else statement and text “Value of x is less than 5” is printed.
The else-if Statement in Java Language
The else-if statments can be nested. The last else is optional and executes when all previous expressions are false. The syntax of else-if construct is as follow:
if (expression1){
statements;
}
else if (expression2){
statements;
}
else {
statements;
}
The following else-if code displays the grade depending upon the value of variable "marks".
int marks = 75;
if (marks > 90){
System.out.println("A Grade");
}else if (marks > 80){
System.out.println("B Grade");
}else if (marks > 70){
System.out.println("C Grade");
}else if (marks > 60){
System.out.println("D Grade");
}else{
System.out.println("Fail");
}
//Output: C Grade