Start learning free

LESSON 199 OF 667 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in C++: Letter Grades with if / else if / else

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 cout << "Grade: " << grade << endl;

Declare string grade; first, then assign in an if/else if chain.

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";
}
cout << "Grade: " << grade << endl;

STEP 1 OF 7
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in C++, explained

    Problem overview

    Version 3 turns a numeric score into a letter grade. The starter code has int score = 85;, and the program prints Grade: followed by the letter. 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, so exactly 90 is an A and 59 is an F.

    For 85 the program prints:

    Grade: B
    

    The earlier versions did arithmetic only. This one makes the program choose between several results.

    Approach: test the ranges from the top down

    A chain of if, else if and else tries its conditions in order and runs the block of the first one that is true. After that block, the rest of the chain is skipped. This lets each condition rely on the ones before it.

    If you start with the highest grade, a score that reaches the second test is already known to be below 90. So the second condition only needs score >= 80, not score >= 80 && score < 90. The same holds down the chain, and the final else has no condition at all: it catches whatever is below 60.

    The order is part of the logic. If you tested score >= 60 first, a score of 95 would stop there and get a D.

    Each else if is really an else whose body is another if statement. C++ lets you leave out the braces around a body that holds a single statement, and that is why the chain looks flat on the page even though each test sits inside the else of the one before it. Knowing this explains the rule that a branch runs only when everything above it was false.

    The alternative is to write both limits in every condition, for example score >= 80 && score < 90. That also works, but it is longer, and the two limits have to stay consistent whenever the scale changes. In the top-down chain each boundary appears once.

    Step by step

    1. Declare grade as a string before the chain. A string without an initial value starts out empty. It must live outside the branches: a variable declared inside a { } block disappears at the closing brace, so the print at the end couldn't see it.
    2. Open the chain with if (score >= 90) and assign "A" inside the block.
    3. Add else if (score >= 80) for "B". It sits on the same line as the closing brace of the block before it, which is the usual C++ style.
    4. Add else if (score >= 70) for "C".
    5. Add else if (score >= 60) for "D".
    6. Finish with a plain else that assigns "F", and close the chain.
    7. Print after the chain, so the line runs once with whatever grade the chain chose.

    The solution in C++

    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";
    }
    cout << "Grade: " << grade << endl;
    

    Assign with = and compare with >=. Writing = inside a condition is a classic slip: if (score = 90) compiles, assigns 90 to score and is always true.

    Because exactly one branch runs, grade always gets a value, and the final else guarantees it even for scores that are negative or zero. A string literal like "A" uses double quotes, and assigning it to a string copies the text into the variable.

    Following the chain on a few scores

    Here is which test first succeeds for some scores, and the grade that results:

    scorefirst true testgrade
    100score >= 90A
    89score >= 80B
    79score >= 70C
    60score >= 60D
    59none, so the else runsF

    Each row skips every test after the first true one. For 89, the first test fails because 89 is below 90, and the second succeeds, so the "C", "D" and "F" assignments never run.

    The empty string matters too. If a branch were missing, grade would stay empty and the program would print Grade: with nothing after it. The closing else prevents that.

    Edge cases and common mistakes

    • The boundaries. Use >=, not >. With >, a score of exactly 90 would fall to B. Check 90, 89, 80, 79, 70, 69, 60 and 59: they give A, B, B, C, C, D, D and F.
    • Wrong order. Testing the lowest range first catches every higher score too.
    • Declaring grade inside a branch. It is gone after the closing brace, and the print then fails to compile.
    • A separate if for each range. Several independent if statements all run their tests, so a score of 95 would be assigned A, then B, then C. The else keeps the branches exclusive.
    • Extreme scores. 100 gives A and 0 gives F.

    Testing

    With score = 85 the output is Grade: B. Change the score to each boundary value above and compare it with the scale. The edge values are where most mistakes show up.

    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free