What is the ‘nested if’ statement in Java and how to use it?

Java 8Object Oriented ProgrammingProgramming

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

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

Example

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


raja
Published on 08-Feb-2018 08:28:39
Advertisements