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.

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");
        }
    }
}
 
To download raw file Click Here

Basic Programs



Learn All in Tamil © Designed & Developed By Tutor Joes