When you place an if-else statement inside another if or else block, it is called nested if-else in Java. A nested if-else statement means placing an if-else statement inside another if-else block. This allows you to evaluate multiple conditions one after another in a structured way. It enables you to precisely control over program flow.
Nested if-else statements are especially useful in Java programming when decisions depend on multiple interrelated criteria.

Basic Syntax of Nested If-Else in Java
The general syntax for defining nested if-else statements in Java is as:
if (condition1) {
 // Executes when condition1 is true
    if (condition2) {
        // Executes when both condition1 and condition2 are true
    } else {
        // Executes when condition1 is true but condition2 is false
    }
} else {
    // Executes when condition1 is false
}In the above syntax of nested if-else statements,
- The outer if statement checks the first condition (condition1).
- If the first condition evaluates to true, the inner if statement checks another condition (condition2). If the condition2 is true, the inner if block will execute. Otherwise, the inner else will execute if the condition2 is false.
- The outer else block will execute if condition1 is false.
Basic Example of Nested If-Else in Java
Let’s start with a simple example to understand how nested if-else works.
Example 1: Check if a number is positive, negative, or zero
public class NestedIfElseEx1 {
  public static void main(String[] args) {
     int number = -10;
  // Nested if-else statements.
     if (number >= 0) {
        if (number == 0) {
             System.out.println("The number is zero.");
        } else {
             System.out.println("The number is positive.");
          }
     } else {
            System.out.println("The number is negative.");
      }
  }
}Output:
The number is negative.In this example,
- The outer if statement checks whether the number is non-negative (number >= 0). If it evaluates to true, the inner if statement checks whether it is zero.
- If the condition of the inner if statement is not zero, the inner else block executes and prints the statement “The number is positive.”.
- If the outer if condition evaluates to false, the outer else block executes and displays the statement “The number is negative.”.
Example 2: Find the Largest of Three Numbers
A common use case of nested if-else statements in Java is to find the largest among three numbers. Let’s write a program based on it.
public class LargestNumber {
  public static void main(String[] args) {
     int a = 20, b = 50, c = 30;
     if (a > b) {
        if (a > c) {
             System.out.println(a + " is the largest number.");
        } else {
             System.out.println(c + " is the largest number.");
          }
     } else {
         if (b > c) {
              System.out.println(b + " is the largest number.");
         } else {
              System.out.println(c + " is the largest number.");
          }
      }
   }
}Output:
50 is the largest number.In this example,
- The outer if condition (a > b) compares a and b.
- If this condition evaluates to true, the inner if condition checks a and c.
- If the inner condition evaluates to false, the outer else checks b and c.
- Finally, the program prints the largest number.
Example 3: Student Grade Evaluation
Let’s take an example in which we will use nested if-else statements for a grading system.
public class GradeCheck {
   public static void main(String[] args) {
      int marks = 85;
      if (marks >= 90) {
           System.out.println("Grade: A+");
      } else {
           if (marks >= 80) {
               System.out.println("Grade: A");
           } else {
               if (marks >= 70) {
                   System.out.println("Grade: B");
               } else {
                   if (marks >= 60) {
                        System.out.println("Grade: C");
                    } else {
                        System.out.println("Grade: Fail");
                    }
                }
            }
        }
    }
}Output:
Grade: AExample 4: Nested If-Else for Leap Year Checking
Let’s write a Java program in which we will use nested if-else in Java for checking leap years.
public class LeapYearCheck {
  public static void main(String[] args) {
     int year = 2024;
     if (year % 4 == 0) {
         if (year % 100 == 0) {
             if (year % 400 == 0) {
                  System.out.println(year + " is a leap year.");
             } else {
                  System.out.println(year + " is not a leap year.");
               }
          } else {
               System.out.println(year + " is a leap year.");
           }
      } else {
          System.out.println(year + " is not a leap year.");
      }
   }
}Output:
2024 is a leap year.A year is a leap year if:
- It’s divisible by 4, and
- If divisible by 100, then it must also be divisible by 400.
- This multi-level decision perfectly fits nested if-else logic.
Advanced Example 5: Employee Bonus Calculation
public class EmployeeBonus {
  public static void main(String[] args) {
     double salary = 50000;
     int yearsOfService = 7;
     if (yearsOfService >= 5) {
        if (salary < 40000) {
             System.out.println("Bonus: 20% of salary");
        } else if (salary >= 40000 && salary < 60000) {
              System.out.println("Bonus: 15% of salary");
         } else {
              System.out.println("Bonus: 10% of salary");
          }
        } else {
            System.out.println("No bonus for less than 5 years of service.");
        }
    }
}Output:
Bonus: 15% of salaryBest Practices for Using Nested If-Else in Java
There are the following key points that you should always keep in mind while using nested if-else in Java program.
- Use indentation properly because it improves readability.
- Avoid too many nested levels because deep nesting makes code hard to read.
- Use logical operators (&&, ||) when possible to simplify conditions.
- Consider switch-case or if-else ladders if conditions are too many.
Common Mistakes in Nested If-Else
There are some common mistakes that beginners always do while using nested if-else statements:
- Forgetting braces {}- Always use braces to avoid logical errors.
 
- Misplaced else block- The else block always pairs with the nearest unmatched if.
 
- Over-nesting- Too many nested levels make code unreadable. So, always use combined conditions instead.
 
- Redundant conditions- Avoid checking conditions that are already implied by earlier checks.
 
Programming Exercises: Nested If-Else Practice
1. Leap Year Checker:
- Write a Java program that checks if a year is a leap year. Consider nested conditions for divisibility by 4, 100, and 400.
2. ATM Withdrawal Validation:
- Write a Java program to validate a withdrawal request based on account balance, daily limit, and PIN correctness.
3. Ticket Pricing:
- Write a Java program to calculate ticket prices with discounts for children (<12), seniors (>60), and groups larger than 10.
4. Determine Type of Triangle:
- Write a Java program to determine the type of triangle (Equilateral, Isosceles, or Scalene) based on three sides
Frequently Asked Questions (FAQ)
1. What is a nested if-else in Java?
A nested if-else means an if-else statement inside another if or else block. It’s used for multi-level decision-making.
2. Can we use multiple nested if-else statements in Java?
Yes, you can nest multiple if-else blocks, but keep it readable and logical.
3. What’s the difference between nested if-else and if-else-if ladder?
Nested if-else: One if-else inside another.
If-else-if ladder: Multiple conditions checked sequentially in a single structure.
4. How can I reduce deep nesting in Java?
Use logical operators (&&, ||) or switch statements to simplify conditions.
5. Can we have an inner if without braces?
Yes, but not recommended. Always use braces {} for clarity and maintainability.
6. Can nested if-else return a value in methods?
Yes. You can use return inside any if or else block if the code is inside a method.
Conclusion
Nested if-else statements in Java are powerful concept when you need to check multiple related conditions. They form the backbone of complex decision-making in Java programs. However, always write them cleanly and clearly because excessive nesting can quickly make your code difficult to understand.
If you practice the above nested if-else examples, you’ll easily handle real-world logic like grading systems, eligibility checks, and business rules in Java.





