Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - nested if statement



It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.

Syntax of Nested if Statement

The syntax for a nested if...else is as follows −

if(Boolean_expression 1) {
   // Executes when the Boolean expression 1 is true
   if(Boolean_expression 2) {
      // Executes when the Boolean expression 2 is true
   }
}

You can nest else if...else in the similar way as we have nested if statement.

Working of Nested if Statement

The concept nested if statement refers to testing the condition(s) inside a condition. The working of a nested if statement is quite easy, the inner condition is checked only when the outer condition is true.

For example, there are two conditions to be evaluated condition_1 and condition_2. We need to check condition_2 only if condition_1 is true. In this case, we will write condition_2 (with its block) as the block/body of the condition_1. Thus, condition_1 will be considered as an outer condition and condition_2 will be considered as an inner condition.

Consider the below syntax -

// Outer condition
if (condition_1) {
  // Inner condition
  if (condition_2) {
  ...
  }
}

Java Nested if Statement Examples

Example 1

In this example, we're showing use of nested if statement within an if statement. We've initialized two variables x and y to 30 and 20 respectively. Then we're checking value of x with 30 using if statement. As if statement is true, in its body we're again checking value of y using a nested if statement.

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");
         }
      }
   }
}

Output

X = 30 and Y = 10

Example 2

In this example, we're showing use of nested if statement within an else statement. We've initialized two variables x and y to 30 and 20 respectively. Then we're checking value of x less than 30 using if statement. As if statement is false, control jumps to else statement where we're again checking value of y using a nested if statement.

public class Test {

   public static void main(String args[]) {
      int x = 30;
      int y = 10;

      if( x < 30 ) {
         System.out.print("X < 30");
      } else {
         if( y > 9 ) {
            System.out.print("X > 30 and Y > 9");
         }  
      }
   }
}

Output

X > 30 and Y > 9

Example 3

In this example, we're showing use of nested if statement within an else statement. We've initialized two variables x and y to 30.0 and 20.0 respectively. Then we're checking value of x less than 30.0 using if statement. As if statement is false, control jumps to else statement where we're again checking value of y using a nested if statement.

public class Test {

   public static void main(String args[]) {
      double x = 30.0;
      double y = 10.0;

      if( x < 30.0 ) {
         System.out.print("X < 30.0");
      } else {
         if( y > 9.0 ) {
            System.out.print("X > 30.0 and Y > 9.0");
         }  
      }
   }
}

Output

X > 30.0 and Y > 9.0
java_decision_making
Advertisements