Start learning free

LESSON 199 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in Java: Letter Grade with if / else if

Practice Read the full explanation

Use if/else if/else to turn a numeric score into a letter grade.

You start with:

int score = 85;

Build:

Grade: B

Rules:

  • score >= 90 is "A"; 80-89 is "B"; 70-79 is "C"; 60-69 is "D"; below 60 is "F".
  • The boundaries count: a score of exactly 90 is an A, exactly 80 a B, exactly 70 a C, exactly 60 a D, and 59 is an F.
  • Print "Grade: " followed by the letter grade, for example System.out.println("Grade: " + grade);

Declare String grade; outside the chain so all branches can assign to it.

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);

STEP 1 OF 7
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in Java, explained

    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

    1. Declare grade without a value. String grade; goes below score and 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.
    2. Start with the A branch. if (score >= 90) sets grade to "A". The closing brace goes on the same line as the next else.
    3. Add the B branch. else if (score >= 80) sets "B". Only scores below 90 reach this test.
    4. Add the C branch for 70 or more.
    5. Add the D branch for 60 or more.
    6. Finish with a plain else. It sets "F" and closes the chain. Because every branch, including this one, assigns grade, Java accepts the later use of the variable.
    7. Print after the chain. The println line 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 that grade is set on every path.
    • The variable's scope. A variable lives inside the braces where it is declared. That is why grade is declared before the chain.
    • else if is an else followed by an if. There is no separate keyword. Each else holds one more if, 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 a String. Single quotes would make a char, which is a different type from the one grade holds.

    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 > 90 a score of exactly 90 would fall down to B.
    • Missing else. Without the final else, a score below 60 leaves grade unset, and the program fails to compile with the error variable grade might not have been initialized.
    • Declaring inside a branch. Writing String grade = "A"; inside the braces and using grade after the chain gives the compile error cannot find symbol.
    • Separate if statements. A series of plain if statements tests every condition, so a score of 95 would match several of them and the last one would win. Chain them with else 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:

    ScoreGrade
    100A
    90A
    89B
    80B
    79C
    70C
    69D
    60D
    59F
    0F
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free