Provide Best Programming Tutorials

nested-if Statement In Java

nested-if

A nested if is an if statement that is the target of another if or else. Nested if statements mean an if statement inside an if statement. Yes, Java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.

Syntax:

if (condition1) {
   // Executes when condition1 is true
   if (condition2) {
      // Executes when condition2 is true
   }
}

The code below will print a<b is true.

public static void showNestedIf() {
   int a = 10;
   int b = 20;
   if (a == 10) {
     if (a < b) {
       System.out.println("a<b is true");
     } else {
       System.out.println("a<b is false");
     }
   }
 }

Leave a Reply

Close Menu