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.

if (outerCondition) { // Outer if block if (innerCondition1) { // Inner if block // Code to execute if innerCondition1 is true } else { // Code to execute if innerCondition1 is false } } else { // Code to execute if outerCondition is false }








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

import java.util.Scanner; public class NestedIfExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); if (number > 0) { System.out.println("The number is positive."); } else { if (number < 0) { System.out.println("The number is negative."); } else { System.out.println("The number is zero."); } } } }










In this example:

  1. We use an if statement to check if the number is greater than 0. If it is, we print "The number is positive."
  2. We use an else if statement to check if the number is less than 0. If it is, we print "The number is negative."
  3. 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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

Popular posts from this blog

Lecture 2 - How to Take Input From User in Java

Programming Conventions in Java

Why Java is Called Java?