Nested if statement in Java
In Java, you can use nested if statements to create more complex conditional logic by placing one if statement inside another. This allows you to check multiple conditions in a structured manner.
Here's the basic syntax of a nested if statement:
Here if condition1 is true then all the code within if block (line 2 to 7) will be executed. In the if block there is another if on line 3 having condition2. If this condition is true then its block (line 4) will be executed.
It means that for statement written on line 4 both condition1 and condition2 needs to be true while statement written on line2, line 6 and line 7 will be executed if condition1 is true.
Below is syntax of nested if statement having else blocks.
Here's a breakdown of the above:
- if: This is the keyword that initiates the outer if statement.
- outerCondition: This is the condition to be evaluated by the outer if statement.
- {}: These curly braces enclose the code block associated with the outer if statement.
- innerCondition1: This is the condition to be evaluated by the inner if statement, which is nested inside the outer if statement.
- else: This is an optional keyword that specifies an alternative code block to execute if the inner condition (innerCondition1) is false.
- else: This is another optional keyword that specifies an alternative code block to execute if the outer condition (outerCondition) is false.
EXAMPLE
In this example:
- We use an if statement to check if the number is greater than 0. If it is, we print "The number is positive."
- We use an else if statement to check if the number is less than 0. If it is, we print "The number is negative."
- If neither of the above conditions is met, we use an else statement to print "The number is zero."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IMPORTANT:
CLICK HERE for if-else-if statement
CLICK HERE for switch statement
EXERCISES
Nested if statements in Java are used when you have conditional statements inside other conditional statements. Here are some exercises to practice working with nested if statements:
- Grading System: Create a program that takes a student's score as input and, based on certain conditions, assigns a letter grade (A, B, C, etc.). Use nested if statements to handle different grade boundaries.
- BMI Categories: Write a program that calculates the Body Mass Index (BMI) of an individual and then categorizes them as underweight, normal weight, overweight, or obese based on BMI ranges. Use nested if statements to determine the category.
- Ticket Pricing: Create a program for a movie theater that calculates the ticket price based on age and time of day. Use nested if statements to apply different pricing rules for children, seniors, and different showtimes.
- Password Strength Checker: Develop a program that evaluates the strength of a password entered by a user. Use nested if statements to check for criteria like length, the presence of special characters, numbers, and uppercase letters. Provide different messages based on the strength.
- Nested Menu Selection: Implement a text-based menu system with multiple levels of options. Users should be able to navigate through different menus and submenus using nested if statements.
- Triangle Type Checker: Write a program that takes three sides of a triangle as input and determines whether it's an equilateral, isosceles, or scalene triangle. Use nested if statements to handle the different cases.
- Credit Card Validator: Create a program that validates credit card numbers using the Luhn algorithm. Use nested if statements to check if the card number is valid or not.
- Library Fine Calculator: Build a program that calculates the late fee for a library book based on the number of days it's overdue and the type of book (e.g., regular, children's, reference). Use nested if statements to apply different fine rates.
- Airline Ticket Booking: Develop a program for an airline reservation system that calculates the ticket price based on factors like class (first class, business, economy), round trip or one-way, and age of the passenger. Use nested if statements to compute the total cost.
These exercises will help you become proficient in using nested if statements to handle more complex decision-making scenarios in your Java programs.
Comments
Post a Comment