Nested If in Java


A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. when you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else.

Syntax:
if(Expression 1)
{
    // Executes when the Expression 1 is true
     if(Expression 2)
     {
        // Executes when the Expression 2 is true
     }
}

This Java program checks whether a driver is eligible for insurance based on their marital status, gender, and age. The program uses nested if statements to check for different conditions based on the driver's marital status.

If the driver is unmarried (marital variable equals 'U' or 'u'), the program prompts the user to enter their gender and age. If the driver is male and over 30 or female and over 25, the program prints "You are Eligible for Insurance". Otherwise, the program prints "You are Not Eligible for Insurance".

If the driver is married (marital variable equals 'M' or 'm'), the program assumes that they are eligible for insurance and prints "You are Eligible for Insurance". If the marital variable is neither 'M' nor 'U', the program prints "Invalid Input".

Overall, this program provides a simple example of using nested if statements to check multiple conditions and perform conditional operations in Java.

Source Code

import java.util.Scanner;
 
public class nested_if {
    public static void main(String args[])
    {
        /*
            Nested if Statement
            A company insures its drivers in the following cases:
                a.	If the driver is married.
                b.	If the driver is unmarried, male & above 30 years of age.
                c.	If the driver is unmarried, female & above 25 years of age.
        */
        Scanner in =new Scanner(System.in);
        System.out.println("Enter The Marital Status M/U: ");
        char marital=in.next().charAt(0);
        if(marital=='u' || marital=='U' )
        {
            System.out.println("Enter The Gender M/F: ");
            char gender=in.next().charAt(0);
            System.out.println("Enter The Age : ");
            int age=in.nextInt();
            if((gender=='M'||gender=='m')&& age>=30)
            {
                System.out.println("You are Eligible for Insurance");
            }
            else if((gender=='F'||gender=='f')&& age>=25)
            {
                System.out.println("You are Eligible for Insurance");
            }
            else
            {
                System.out.println("You are Not Eligible for Insurance");
            }
 
        }
        else if(marital=='m' || marital=='M' )
        {
            System.out.println("You are Eligible for Insurance");
        }
        else {
            System.out.println("Invalid Input");
        }
    }
}
 

Output

Enter The Marital Status M/U:
u
Enter The Gender M/F:
f
Enter The Age :
23
You are Not Eligible for Insurance
To download raw file Click Here

Basic Programs