DEV Community

Ranjith Ranjith
Ranjith Ranjith

Posted on

NESTED IF ELSE

Nested if else
* one if statement inside another if statement * If the outer condition is true the inner conditions are checked and executed accordingly.
* Nested if condition comes under decision-making statement in Java, enabling multiple branches of execution.

Note:

 *Normal if condition checks condition independently which means each condition works on its own.
Enter fullscreen mode Exit fullscreen mode
  • Whereas nested if checks conditions that depend on each other, which means one condition is only checked if another condition is true

Children (under 13 years): Rs. 100 on weekdays, Rs. 120 on weekends.
Adults (13 to 64 years): Rs. 150 on weekdays, Rs. 180 on weekends.
Seniors (65 years and above): Rs. 130 on weekdays, Rs. 150 on weekends.
public class MovieTicketPricing {
Ex
public static void main(String[] args) {
int age = 30; // Customer's age
boolean isWeekend = false; // Is it a weekend?

    if (age  <  13) {
        if (isWeekend) {
            System.out.println("Ticket Price: Rs. 120");
        } else {
            System.out.println("Ticket Price: Rs. 100");
        }
    } else if (age  < = 64) {
        if (isWeekend) {
            System.out.println("Ticket Price: Rs. 180");
        } else {
            System.out.println("Ticket Price: Rs. 150");
        }
    } else {
        if (isWeekend) {
            System.out.println("Ticket Price: Rs. 150");
        } else {
            System.out.println("Ticket Price: Rs. 130");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}
Reffer::
https://www.shiksha.com

Top comments (0)