Nested if else statement in Java
In Java, a nested if-else statement is a construct that allows you to place one if-else statement inside another if-else statement. It provides a way to create more complex conditional logic based on multiple conditions. The nested if-else statement is useful when you need to evaluate multiple conditions sequentially and perform different actions based on those conditions.
Syntax:
if (condition1) { // code will be executed if condition one is true if (condition2) { // both conditions are true then the code will be executed } else { // code executes if condition one is true and the second condition is false } } else { // code executes if first condition is false }
Example 1:
In this example, the program asks the user for their age. Based on the age provided, it checks whether the person is eligible to vote. If the age is greater than or equal to 18, the program enters the outer if statement and prints "You are eligible to vote." By using nested if-else statements, this example demonstrates how different actions are performed based on multiple conditions.
FileName: NestedIfElseExample.java
import java.util.Scanner; public class NestedIfElseExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //user enter their age System.out.print("Enter your age: "); int age = scanner.nextInt(); if (age >= 18) { // If age is 18 or above, the person is eligible to vote System.out.println("You are eligible to vote."); // Ask the user if they have a valid ID System.out.print("Do you have a valid ID? (yes/no): "); String idResponse = scanner.next(); if (idResponse.equalsIgnoreCase("yes")) { // If the user has a valid ID, they can proceed to vote System.out.println("You can proceed to vote."); } else { // If the user doesn't have a valid ID, they cannot vote System.out.println("you are not eligible for vote because you don’t have valid ID card."); } } else { // If age is below 18, the person is not eligible to vote System.out.println("You are not eligible to vote."); } scanner.close(); } }
Output:
Enter your age: 21 You are eligible to vote. Do you have a valid ID? (yes/no): yes You can proceed to vote.
Example 2:
In this example, the program asks the user to enter their age and income. Based on these inputs, the program determines whether the user is eligible for a loan using a nested if-else statement. By utilizing a nested if-else statement, this example demonstrates how to evaluate multiple conditions to determine loan eligibility based on age and income.
FileName: NestedIfElseExample.java
import java.util.Scanner; public class NestedIfElseExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // user to enter their age and income System.out.print("Enter your age: "); int age = scanner.nextInt(); System.out.print("Enter your income: "); double income = scanner.nextDouble(); // Check eligibility for a loan using nested if-else statements if (age >= 18) { if (income >= 30000) { System.out.println("Congratulations! You are eligible for a loan."); } else { System.out.println("Sorry, you are not eligible for the Loan."); } } else { System.out.println("Sorry, you must be at least 18 years old to apply for a loan."); } scanner.close(); } }
Output:
Enter your age: 21 Enter your income: 45000 Congratulations! You are eligible for a loan.
Example 3
In the given program, the program asks the user to enter a number. Based on the number provided, the program evaluates multiple conditions using nested if-else statements. By utilizing multiple nested if-else statements, this example demonstrates how to evaluate multiple conditions in a hierarchical manner to provide specific outputs based on the user's input.
FileName: MultipleNestedIfElseExample.java
import java.util.Scanner; public class MultipleNestedIfElseExample { 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) { if (number % 2 == 0) { if (number > 100) { // Positive, even, and greater than 100 System.out.println("The Entered number is positive, even, and greater than 100."); } else { // Positive, even, and not greater than 100 System.out.println("The Entered number is positive, even, and not greater than 100."); } } else { if (number > 50) { // Positive, odd, and greater than 50 System.out.println("The Entered number is positive, odd, and greater than 50."); } else { // Positive, odd, and not greater than 50 System.out.println("The Entered number is positive, odd, and not greater than 50."); } } } else if (number < 0) { if (number % 2 == 0) { if (number < -100) { // Negative, even, and less than -100 System.out.println("The Entered number is negative, even, and less than -100."); } else { // Negative, even, and not less than -100 System.out.println("The Entered number is negative, even, and not less than -100."); } } else { if (number < -50) { // Negative, odd, and less than -50 System.out.println("The Entered number is negative, odd, and less than -50."); } else { // Negative, odd, and not less than -50 System.out.println("The Entered number is negative, odd, and not less than -50."); } } } else { // The number is zero System.out.println("The number is zero."); } scanner.close(); } }
Output:
Enter a number: 102 The Entered number is positive, even, and greater than 100.