Nested If

What is the Nested if statement in Java?

The nested if statement represents the if block within another if block. In this case, only when the outer if block condition is true does the inner if block condition execute. When there is an if statement inside another if statement, it is called a nested if statement.

Syntax:
if( condition ) {
    if( condition ) {
      // code to be executed
      else
     // code to be executed
    }
  }
  else {
    // code to be executed
  }
Explanation of this code:

In this code, there are two levels of if statements, forming a nested if statement. Let’s break it down:

  1. The outer if statement checks the condition. If the condition is true, the program will enter the block associated with the outer if.
  2. Inside the block of the outer if, there is another if statement (inner if). The inner if statement checks its condition. If the inner if condition is true, the program will execute the code inside the inner if block.
  3. If the inner if condition is false, the program will execute the code in the “else” part of the inner if statement (the “else” branch is optional, as you have it in the comment, but it’s not visible in the provided code).
  4. If the outer if condition is false, the program will execute the code inside the “else” block of the outer if statement.

Java nested if Statement Example

Example 1:

package com.example.myapp; // Package name

public class NestedIf {
    public static void main(String[] args) {
        // Requirement: if age is greater than 18 then if age is less than 48 print "Adult" else print "Old"
        int age;
        age = 50;
        if (age > 18) {
            if (age < 48)
                System.out.println("Adult");
            else
                System.out.println("Old");
        }
    }
}
Output:
Old
Explanation of this code:
  1. int age; – This line declares a variable named age of integer type, but it does not assign any value to it yet.
  2. age = 50; – This line assigns the value 50 to the variable age, representing the age of an individual.
  3. The code uses an if statement to check whether the age is greater than 18.
  4. Inside the first if block (if (age > 18)), there is another nested if statement to check whether the age is less than 48.
  5. If both conditions are satisfied (age > 18 and age < 48), the code inside the nested if block is executed, which prints “Adult” to the console.
  6. If the condition in the nested if statement (age < 48) is not satisfied, the code inside the else block of the nested if statement is executed, which prints “Old” to the console.
  7. If the condition in the outer if statement (age > 18) is not satisfied, the entire inner if-else block is skipped, and no output is produced.

In summary, the code checks if the age is greater than 18. If it is, then it checks if the age is less than 48. If both conditions are met, it prints “Adult.” If the first condition is met (age > 18) but the second condition is not met (age >= 48), it prints “Old.” If the age is not greater than 18, no output is produced.

In the given example, the age is 50, which is greater than 18 but also greater than 48, so the output will be “Old.”

Example 2:

Write a program that solves this requirement :

If the student attends more than 75 classes, then print “Qualified for the exam.” If the student attends more than 75 but less than 90 classes, then print “Obtain 75% marks.” If the student attends more than 90 classes, then print “He is getting 100% marks.” Otherwise, print “He is not qualified.”

But if the student attends more than 64 classes but less than 74 classes, then print “Send the application for the exam” and “Getting 65% marks.”

package com.example.myapp; // Package name
public class StudentAttendance {
    public static void main(String[] args) {
        int attends = 80; // Change the attendance value to test different scenarios

        if (attends > 75) {
            System.out.println("Qualified for the exam.");

            if (attends > 75 && attends < 90) {
                System.out.println("Obtain 75% marks.");
            }

            if (attends > 90) {
                System.out.println("He is getting 100% marks.");
            }
        } else {
            System.out.println("He is not qualified.");

            if (attends > 64 && attends < 74) {
                System.out.println("Send the application for the exam.");
                System.out.println("Getting 65% marks.");
            }
        }
    }
}
Output:
Qualified for the exam.</br >Obtain 75% marks.
Explanation of this code:
  1. The code defines a class named StudentAttendance and a main method within it, which is the entry point for the program.
  2. An integer variable attends is declared and assigned a value of 80. This variable represents the number of classes attended by a student. You can change this value to simulate different scenarios.
  3. The code uses an if statement to check whether the attends variable has a value greater than 75. If this condition is true, the code block within the first set of curly braces is executed.
  4. Inside the first if block, it prints “Qualified for the exam.” Then, it checks further conditions using nested if statements.
  5. The second nested if statement checks whether the attends variable is greater than 75 and less than 90. If this condition is true, it prints “Obtain 75% marks.”
  6. The third nested if statement checks whether the attends variable is greater than 90. If this condition is true, it prints “He is getting 100% marks.”
  7. If the initial condition in the outer if statement (attends > 75) is false, the code enters the else block.
  8. Inside the else block, it prints “He is not qualified.”
  9. Within the else block, there’s another if statement that checks whether the attends variable is greater than 64 and less than 74. If this condition is true, it prints “Send the application for the exam.” Additionally, it prints “Getting 65% marks.”

In summary, the code simulates a scenario where a student’s qualification for an exam and their potential marks are determined based on the number of classes attended (attends). It prints appropriate messages based on the conditions specified, including checks for attendance ranges and qualification thresholds.

Visited 1 times, 1 visit(s) today

Comments are closed.

Close