Problem overview
Version 3 turns a numeric score into a letter grade. The scale is: 90 or more is "A", 80 to 89 is "B", 70 to 79 is "C", 60 to 69 is "D", and anything below 60 is "F". The boundaries belong to the higher grade: exactly 90 is an A, exactly 60 is a D and 59 is an F.
The program starts with int score = 85; and prints Grade: B.
Approach: a ladder from the top grade down
An if / else if / else chain tests its conditions in order and runs only the first block whose condition is true. All the later branches are skipped. That behaviour is what makes a compact grading ladder possible.
Start with the highest grade. If score >= 90 is false, the score is already known to be below 90, so the next test only needs a lower limit: score >= 80 means 80 to 89. The same reasoning repeats for 70 and for 60. The final else has no condition and catches everything that is left, which is every score below 60.
The branches only choose a value. A variable declared before the chain holds the result, and one println after the chain prints it.
How the chain runs for 85
Java first asks whether 85 is at least 90. It is not, so the A block is skipped and the program moves on to the next test. Is 85 at least 80? Yes, so grade becomes "B". The tests for 70 and 60 and the else are never evaluated, because the chain ends at the first branch that ran. The program then continues after the closing brace of the whole chain and prints Grade: B.
A score of 59 fails all four tests, so the else runs and the output is Grade: F.
Step by step
- Declare
gradewithout a value.String grade;goes belowscoreand outside the chain. A variable created inside a branch would stop existing at that branch's closing brace, so the print line couldn't use it. - Start with the A branch.
if (score >= 90)setsgradeto"A". The closing brace goes on the same line as the nextelse. - Add the B branch.
else if (score >= 80)sets"B". Only scores below 90 reach this test. - Add the C branch for 70 or more.
- Add the D branch for 60 or more.
- Finish with a plain
else. It sets"F"and closes the chain. Because every branch, including this one, assignsgrade, Java accepts the later use of the variable. - Print after the chain. The
printlnline sits outside the braces, so it runs once, after a grade has been chosen.
The solution in Java
int score = 85;
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else if (score >= 60) {
grade = "D";
} else {
grade = "F";
}
System.out.println("Grade: " + grade);
What to know about the Java details:
- Definite assignment. A local variable has no default value, and Java refuses to compile code that might read it before it is assigned. The chain ends in an
else, so exactly one branch always runs, and the compiler can see thatgradeis set on every path. - The variable's scope. A variable lives inside the braces where it is declared. That is why
gradeis declared before the chain. else ifis anelsefollowed by anif. There is no separate keyword. Eachelseholds one moreif, which is why the whole ladder behaves like a single decision with one outcome. The lesson uses braces on every branch, and that is the safe habit even when a branch has one statement.- Strings use double quotes.
"A"is aString. Single quotes would make achar, which is a different type from the onegradeholds.
Edge cases and common mistakes
- Wrong order. If the ladder starts with
score >= 60, a score of 95 stops at the first branch and gets a D. Check the highest limit first. - Boundaries. Use
>=, not>. With> 90a score of exactly 90 would fall down to B. - Missing
else. Without the finalelse, a score below 60 leavesgradeunset, and the program fails to compile with the errorvariable grade might not have been initialized. - Declaring inside a branch. Writing
String grade = "A";inside the braces and usinggradeafter the chain gives the compile errorcannot find symbol. - Separate
ifstatements. A series of plainifstatements tests every condition, so a score of 95 would match several of them and the last one would win. Chain them withelse if. - Scores outside 0 to 100. The ladder does not check that a score is valid. A score of 120 still gets an A, and a score of -5 gets an F, because only the lower limits are tested. The lesson never asks for validation, so the ladder stays as short as it is.
Testing
These scores give these grades:
| Score | Grade |
|---|---|
| 100 | A |
| 90 | A |
| 89 | B |
| 80 | B |
| 79 | C |
| 70 | C |
| 69 | D |
| 60 | D |
| 59 | F |
| 0 | F |
