Java Nested if Statement Example

Java Nested if Statement Example demonstrates how to use nested if statement with an example in Java.

Java Nested if Statement

When there is an if statement inside another if statement then it is called the nested if statement.

Syntax

if(condition_1) {
   Statement1(s);

   if(condition_2) {
      Statement2(s);
   }
}

Java Nested if Statement example

package net.javaguides.corejava.controlstatements.ifelse;

public class NetstedIfStatementExample {
    public static void main(String[] args) {
        int num = 50;
        if (num < 100) {
            System.out.println("number is less than 100");
            if (num == 50) {
                System.out.println("number equal to 50");
                if (num > 40) {
                    System.out.println("number is greater than 40");
                }
            }
        }
    }
}
Output:
number is less than 100
number equal to 50
number is greater than 40

Reference

https://www.javaguides.net/2018/10/java-if-ifelse-nestedif-ifelseif-statement-with-examples.html

Comments