Nested If Else clause in java

java.util.Scanner;
import class GradeAssignment {
  public static void main(String[] args) {
    int marksObtained;
    char grade;
    int passingMarks = 40;

    // Create a Scanner object to read input
    Scanner input = new Scanner(System.in);

    // Prompt user to input marks
    System.out.println("Input marks scored by you");

    // Read the marks obtained
    marksObtained = input.nextInt();

    // Check if the student passed or failed
    if (marksObtained >= passingMarks) {
      // If passed, assign grades based on marks
      if (marksObtained > 90) {
        grade = 'A';
      } else if (marksObtained > 75) {
        grade = 'B';
      } else if (marksObtained > 60) {
        grade = 'C';
      } else {
        grade = 'D';
      }

      // Print the message about passing and the grade
      System.out.println("You passed the exam and your grade is " + grade);
    } else {
      // If failed, assign grade 'F'
      grade = 'F';
      System.out.println("You failed and your grade is " + grade);
    }

    // Close the scanner object to prevent memory leak
    input.close();
  }
}

Let’s walk through the Java code given below with a step-by-step explanation for each part of the code.

Step-by-step Explanation:

1. Importing the ‘Scanner’ class:

import java.util.Scanner;

Allows us to take input from the console using the ‘Scanner’ class from ‘java.util’.

2. Declaration of Class:

class GradeAssignment {

The class ‘GradeAssignment’ has been declared to encapsulate the logic for calculating grades based on marks obtained.

3. Main Method Declaration:

public static void main(String[] args) {

This is the ‘main’ method, where the program begins execution.

4. Declare Variables:

int marksObtained;

char grade;

int passingMarks = 40;

The three variables are declared:

  • ‘marksObtained’: Integer that would take the value for marks provided by the user.
  • ‘grade’: A character variable which takes the assigned grade.
  • ‘passingMarks’: integer initialized with value ’40’, that are minimum marks that could be used for passing.

5. Create a ‘Scanner’ object:

Scanner input = new Scanner(System.in);

A ‘Scanner’ object named ‘input’ is created to read input from the user.

6. Prompt User for Marks:

System.out.println(“Input marks scored by you”);

Prints to the console message ‘ “Input marks scored by you” to ask the user to input marks.

7. Read User Input:

marksObtained = input.nextInt();

It reads the integer value entered by the user and stores it inside the variable named ‘marksObtained’

8. Check Pass or Fail:

if (marksObtained >= passingMarks) {

The program checks if the marks entered by the user are greater than or equal to the passing marks (’40’). If true, the program moves on to calculate the grade. If false, it moves to the ‘else’ block for failing.

9. Assign Grades:

If the student passed, then it gives a grade to the program according to the marks as follows:

  • If marks are greater than ’90’, then grade is ”A”.
  •  If marks are greater than ’75’ and less than or equal to ’90’, then grade is ”B”.
  • If the marks are between ’60’ and ’75’ inclusive, then the grade is ”C”.
  • If the marks are between ’40’ and ’60’ inclusive, then the grade is ”D”.

The corresponding code is as follows:

  if (marksObtained > 90) {

grade = ‘A’;

    } else if (marksObtained > 75) {

        grade = ‘B’;

    } else if (marksObtained > 60) {

        grade = ‘C’;

    } else {

        grade = ‘D’;

    }

10. Print Pass and Grade:   

System.out.println(“You passed the exam and your grade is ” + grade);

If the student passed, this line prints the message showing the student passed and prints the grade that has been assigned.

11. Handle Failure Case:    

else {

grade = ‘F’;

       System.out.println(“You failed and your grade is ” + grade);

}

If the student has not passed (marks are less than 40), the grade ”F” is assigned and the program prints “You failed and your grade is F”.

12. Close the Scanner:

input.close(); 

It is good practice to close the ‘Scanner’ object after it is no longer needed to prevent resource leaks.

Example Execution:

#Example 1: Student Passed with Grade A

Input:

Input marks scored by you

95

The student entered ’95’ as their marks. Since ’95’ is greater than ’90’, the program assigns grade ‘A’.

Output:

You passed the exam and your grade is A

#Example 2: Student Passed with Grade B

Input:

Input marks scored by you

80

The student has put ’80’ as marks. As ’80’ is greater than ’75’ but less than ’90’, the program gives grade ‘B’.

Output:

You passed the exam and your grade is B

#Example 3: Student Passed with Grade C

Input:

Input marks scored by you

65

The student input his marks as ’65’. Since ’65’ is greater than ’60’ but less than or equal to ’75’, the program sets a grade of ‘C’.

Output:

You passed the exam and your grade is C

#Example 4: Student Failed

Input:

Input marks scored by you

30

Student has input his marks as ’30’. Since ’30’ is less than the passing marks (’40’), the program sets a grade of ‘F’.

Output:

You failed and your grade is F

Summary of Output:

1. For input ’95’:

  You passed the exam and your grade is A

2. For input ’80’:

  You passed the exam and your grade is B

3. For input ’65’:

  You passed the exam and your grade is C

4. For input ’30’:

  You failed and your grade is F

Conclusion:

  • The program takes the user’s marks as input and determines whether the student has passed or failed the exam based on a set threshold (passing marks = 40).
  • The final grade is issued based on pass/fail by using predefined ranges. For example, greater than 90 for ‘A,’ greater than 75 for ‘B,’ greater than 60 for ‘C,’ and greater than or equal to 40 for ‘D’. And the grade would be ‘F’ if a student fails.

Leave a Comment

Your email address will not be published. Required fields are marked *