Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Nested If Program

In this program, we will perform various programs using the nested if statement in java. When there is an if statement within another if statement it is known as a nested if statement. There are times when we test a condition within another condition and return one value if the condition is met and return another value if the condition is not met. To evaluate more than one condition and return multiple values depending on the condition nested if statements are used. But before moving forward, if you are not familiar with the concept of nested if statements in java, then do check the article on the topic Conditional Statement in Java.

Syntax

if(condition_1) {
   Statement1(s);

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

Program 1: Java Nested if Program

In this program, we will see the implementation of nested if statements in java.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number.

  4. Ask the user to initialize the number.

  5. Use the first if statement to check if the number is lesser than 100.

  6. Use the inner if statement to check if the number is greater than 50.

  7. If the number is greater than 50 and lesser than 100, then print the message that the entered number is greater than 50 and lesser than 100.

  8. If the number is not greater than 50 but is lesser than 100, then print the message that the entered number is lesser than 100.

  9. If the number is greater than 100 then print the message that the entered number is greater than 100.

  10. Stop.

Below is the Java code example for nested if-else.

//Java nested if Program
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number: ");
        int num=sc.nextInt();
        if( num < 100 )
        { 
           System.out.println("The entered number is less than 100"); 
           if(num > 50)
           {
	          System.out.println("The entered number is greater than 50");
	       }
	    }
	    else
	    {
	        System.out.println("The entered number is greater than 100");
	    }
     }
}


Enter a number: 67
The entered number is less than 100
The entered number is greater than 50

Program 2: Java Nested if-else Program

In this program, we will see the implementation of the nested if-else statements in a java program.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare two variables to store the age and weight of a person.

  4. Ask the user to initialize the age and weight.

  5. Use the first if statement to check if the person is above 18 years of age.

  6. If the person is above 18 years of age then use another if statement to check if the weight of the person is above 50 or not.

  7. If the person's age is above 18 and weight is also above 50 then, print the message that the person is eligible to donate blood.

  8. If the person is above 18 years of age but his/her weight is below 50 then print the message that the person is not eligible to donate blood.

  9. If the person is below 18 years of age, then print the message that the age must be greater than 18.

  10. Display the result.

  11. Stop

Below is the Java code example for nested if-else.

//Java nested if-else Program
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the age: ");
        int age=sc.nextInt();
        System.out.println("Enter the weight: ");
        int weight=sc.nextInt();
        if(age>=18)
        {      
           if(weight>50)
           {    
              System.out.println("The person is eligible to donate blood");    
           }
           else
           {  
              System.out.println("The person is not eligible to donate blood");    
           }  
        }
        else
        {  
            System.out.println("Age must be greater than 18");  
        }  
     }
}


Enter the age: 24
Enter the weight: 49
The person is not eligible to donate blood

Program 3: Java Nested if-else Ladder Program

In this program, we will see the implementation of the nested if-else statements in a java program.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the department name.

  4. Ask the user to initialize the year.

  5. Use the first if statement to check the department of the student.

  6. Use the inner if statement to check in which year the student is.

  7. Display the result.

  8. Stop

Below is the Java code example for nested if-else.

//Java nested if Program
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the dept: ");
        String dept=sc.nextLine();
        int year;
        if(dept.equals("CSE"))
        { 
             System.out.println("Enter a year: ");
             year=sc.nextInt();
           System.out.println("The student is in the Computer Science department"); 
           if(year == 1)
           {
	          System.out.println("The student is in 1st year");
	       }
	       else if(year == 2)
	       {
	        System.out.println("The student is in 2nd year");
	       }
	       else if(year == 3)
	       {
	        System.out.println("The student is in 3rd year");
	       }
	       else if(year == 4)
	       {
	        System.out.println("The student is in 4th year");
	       }
	    }
	    else if(dept.equals("EEE"))
        { 
             System.out.println("Enter a year: ");
             year=sc.nextInt();
           System.out.println("The student is in the Electrical and Electronics department"); 
           if(year == 1)
           {
	          System.out.println("The student is in 1st year");
	       }
	       else if(year == 2)
	       {
	        System.out.println("The student is in 2nd year");
	       }
	       else if(year == 3)
	       {
	        System.out.println("The student is in 3rd year");
	       }
	       else if(year == 4)
	       {
	        System.out.println("The student is in 4th year");
	       }
	    }
	    else if(dept.equals("ME"))
        { 
             System.out.println("Enter a year: ");
             year=sc.nextInt();
           System.out.println("The student is in the Mechanical department"); 
           if(year == 1)
           {
	          System.out.println("The student is in 1st year");
	       }
	       else if(year == 2)
	       {
	        System.out.println("The student is in 2nd year");
	       }
	       else if(year == 3)
	       {
	        System.out.println("The student is in 3rd year");
	       }
	       else if(year == 4)
	       {
	        System.out.println("The student is in 4th year");
	       }
	    }
	    else
	    {
	        System.out.println("Enter a valid department");
	    }
     }
}


Enter the dept: CSE
Enter a year: 2
The student is in the Computer Science department
The student is in 2nd year