Learn if else in java including if else ladder and nested if else conditional statements with simple explanation and program example.
If else in Java programming is a decision-making statement / condition followed by a code block that is used in the program. Depending on evaluation of Boolean expression or conditions to true or false, respective code blocks executed.
In real life we make some decision based on condition. Don’t we? For example,
IF (rain is there)
I will take umbrella
IF (I have $20)
I will by a book
ELSE
I will by later
Similarly, we use conditional statements if or if-else in java programs.
If Statement in java
Syntax of if statement
if(Boolean_expression)
{
// Statements
//do something
}
Java code example
In below program, since expression (a < 20) inside it is true as value of variable a is 10 that is less than 20, the control will go inside the if block and execute its all statements.
public class Sample {
public static void main(String[] args) {
int a = 10;
// If true, go inside the block. if false skip the if block
if (a < 20) {
System.out.println("Inside if block");
}
System.out.println("Done");
}
}
Output:
Inside if block
Done
If the expression (a < 20) is false the code will skip the if block and output of above program will be as below
Output:
Done
WARNING:
if block is written as if (a < 20) {}. If you place semi colon ; after if conditions and before the block e.g. if (a < 20); {}. Then compiler will not give error. it will treat if (a < 20); as a statement.
So, in below code, even though condition is false, control will go inside the block and print the statement. In fact, if we put semi column like if (a < 20);, then if statement and block will be separated.
public class Sample {
public static void main(String[] args) {
int a = 10;
if (a > 20) ;
{
System.out.println("Inside if block");
}
}
}
Output:
Inside if block
Note that if condition is false, still output is printed.
If Else in Java
In if else block, expression in if condition is evaluated to true or false. If expression is true, then control will execute the if block or control will jump to else block and execute it.
Syntax of if else in java:
if(Boolean_expression){
//Executes if block when expression is true
}else{
//Executes else when expression is false
}
Code Example:
In this program, value of a is 10. So, expression in if (a ==10), becomes true. Hence, the control will go inside if block resulting output to Even.
public class Sample {
public static void main(String[] args) {
int a = 10;
if (a == 10) {
System.out.println("Even");
} else {
System.out.println("odd");
}
}
}
Output:
Even
In above program, change value of variable e.g. int a =11, then control will come in else block as in if condition, a ==10 will be false. As an output the program will print Odd.
Output
Odd
If Else Ladder in java
If else ladder has “if, multiple else if and else blocks”. You can see the syntax below.
Syntax:
if(Expression 1){
//Expression 1 is true
}else if(Expression 2){
//Expression 2 is true
}else if(Expression 3){
//Expression 3 is true
}else {
// None of the expression is true
}
The control start checking every if conditions till it find true. once it finds true, it run that if block otherwise it will run else block. It run the else block written in last if no conditions are true.
For example,
first control check if(Expression 1). If true, execute the block and exit the entire if-else ladder.
If the statement if(Expression 1) false, then control check if(Expression 2). If true run the block and exit if else ladder. If false, it check the next.
And so on…..
If none of the conditions / expressions are true, then control goes to last else block.
If Else Ladder Code Example in Java
For better understanding, let’s consider below program, where depending upon the value of day number, it will print the correct day. Day number starts from 1 that represent Monday, 2 represents Tuesday and so on.
First, get overview of the if else ladder code, then let’s test it.
public class Sample {
public static void main(String[] args) {
int day = 2;
// if else if else ladder
if (day == 1) {
System.out.println("Mondyay");
} else if (day == 2) {
System.out.println("Tuesday");
} else if (day == 3) {
System.out.println("Wednesday");
} else if (day == 5) {
System.out.println("Friday");
} else {
System.out.println("Day not found!!!");
}
System.out.println("End");
}
}
Test-1
modify the value of day = 1.
When you run , control will check if (day == 1). It found it true, so, the control execute the code block, print the output as Monday. Exit the entire if else ladder and print “End”
So, Output of the program will be
Mondyay
End
TEST -2
modify the value of day = 3.
control will check if (day == 1). its false, so, control checks if (day == 2). Again false. it checks next if (day == 3). it found true. so, it will print Wednesday and exit the ladder printing “End”
So, Output of the program will be
Wednesday
End
TEST -3
modify the value of day = 9.
Control will check all conditions one by one i.e. if (day == 1)., if (day == 2)., if (day == 3)., if (day == 5).
Since, it found none of the conditions are true, hence it will go to else block, and print Day not found!!!
Output:
Day not found!!!
End
Nested If and Nested If Else in Java
Nested if or nested if else in java programming is simply writing an if or if else conditions inside another if or else block.
We can also say that, writing on if else block inside another if else block. Some time it is referred as outer and inner if else block.
Code example:
public class Sample {
public static void main(String[] args) {
int a = 10;
//Outer if block
if (a < 20) {
//Inner if block
if(a ==10){
System.out.println("Inner if block");
}
System.out.println("Outer if block");
}
}
}
In above program, you can also write inner if else inside an outer if else.
Code example: If else block inside a if block.
public class Sample {
public static void main(String[] args) {
int a = 10;
//Outer if block
if (a < 20) {
//Inner if block
if(a ==10){
System.out.println("Inner if block");
}else{
System.out.println("Inner else block");
}
System.out.println("Outer if block");
}
}
}
NOTE: Note that if outer if condition is false, then compiler will not go inside its block. hence, it can also not go inside the inner if else block.
Recommendation: Copy the above code in eclipse editor. Run and see the outputs. Also, try modifying value of variable a and see the result.