Find Grade From Marks in Java
intermediate· Control Flow · Conditional Statements
Problem
Grades are assigned from a descending set of score thresholds, where the first bracket a score qualifies for determines the grade.
Given a student's marks, determine their letter grade.
Input
marks = 82
Output
Grade: B
Java Program
Java
public class GradeFromMarks {
public static void main(String[] args) {
int marks = 82;
char grade;
if (marks >= 90) { // check from highest to lowest, or a high score would match a lower bracket first
grade = 'A';
} else if (marks >= 75) {
grade = 'B';
} else if (marks >= 60) {
grade = 'C';
} else if (marks >= 40) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade: " + grade);
}
}Output
Grade: B
Core Logic
Testing thresholds from highest to lowest, and stopping at the first one a score satisfies, assigns the grade in a single pass.
How It Works
- 1
marks >= 90is checked first, catching the highest grade band. - 2Each subsequent
else ifchecks the next threshold down — 75, then 60, then 40. - 3The first condition that matches wins, since the
else ifchain skips every later check once one succeeds. - 4Anything that doesn't reach even the lowest passing threshold falls to the final
else, grade F.
For
marks = 82, the >= 90 check fails but >= 75 succeeds, assigning grade B.💡
Key Point: The thresholds must be checked from highest to lowest — checking >= 40 first would incorrectly assign a D to a score of 82, since it satisfies that condition too.
Key Concepts
if / else ifdescending thresholdschar