Start learning free

LESSON 202 OF 289 · 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;

string grade;

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 Console.WriteLine("Grade: " + grade);

grade is already declared in the starter code. Assign to it 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";
}
Console.WriteLine("Grade: " + grade);

STEP 1 OF 6
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in C#, explained

    Problem overview

    This version turns a numeric score into a letter grade. The program starts with int score = 85; and an empty declaration string grade;. It must set grade according to this scale and then print it:

    • 90 or more is "A".
    • 80 to 89 is "B".
    • 70 to 79 is "C".
    • 60 to 69 is "D".
    • Below 60 is "F".

    The boundaries belong to the higher grade: exactly 90 is an A, and 59 is an F. For the starting score of 85 the output is:

    Grade: B

    Approach: test the highest range first

    An if / else if / else chain tests its conditions from top to bottom and runs only the block of the first condition that is true. Everything after that block is skipped. This is what makes the ladder short.

    Start with the highest grade. The first test is score >= 90. If it fails, the score is known to be below 90, so the next test only needs a lower limit: score >= 80 means 80 to 89 here. The same holds one step down for 70 and for 60. Each test relies on the ones above it having failed, so no test needs an upper limit such as score >= 80 && score < 90.

    The final else has no condition. It catches whatever is left, which is every score below 60. Because the chain ends with an else, exactly one branch always runs.

    The direction of the ladder is important. If you test score >= 60 first, a score of 95 is already caught by that test and gets a D, since the tests below it never run. With the tests in the order 60, 70, 80, 90 the program prints Grade: D for 95.

    Following the starting score of 85 through the chain: the test for 90 fails, the test for 80 succeeds, so grade becomes "B" and the remaining tests are never evaluated. A score of 59 fails all four tests and reaches the else, which gives the F. The chain therefore has five possible outcomes and every score ends in exactly one of them, with no gaps and no overlaps between the ranges.

    Step by step

    1. Open the chain with if (score >= 90) and assign "A" to grade inside the braces. Close the block with its }.
    2. Continue with else if (score >= 80) for the B. The else goes on the same line as the closing brace of the previous block, } else if (...) {, which is the style the lesson uses. That is only a convention: C# does not care where the line breaks fall.
    3. Add else if (score >= 70) for the C. Only scores below 80 get this far.
    4. Add else if (score >= 60) for the D. This is the last passing range.
    5. Finish with else, assign "F", and close the whole chain with its final }.
    6. Print after the chain, outside all the braces, with Console.WriteLine("Grade: " + grade);. It runs once, after a grade has been chosen.

    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";
            }
            Console.WriteLine("Grade: " + grade);
    

    Details worth knowing:

    • Definite assignment. string grade; declares a variable without a value. C# lets you read a local variable only when the compiler can prove that every path has assigned it. Because the chain ends with an else, all five paths assign grade, so the print line compiles. If you leave the final else out, the compiler reports error CS0165: Use of unassigned local variable 'grade', because a score below 60 would leave grade without a value.
    • else if is not a separate keyword. It is an else whose body is another if statement. That is why the chain stops at the first true condition.
    • Strings are immutable. The assignment grade = "A" points the variable at a text value. It does not change any existing text.
    • Conditions are bool expressions. C# does not treat numbers as true or false, so if (score) would not compile. Always write the comparison.

    Edge cases and common mistakes

    • Boundaries. Use >=, not >. With score > 90 a score of exactly 90 would fall through to a B.
    • Wrong order. Testing the lowest limit first gives the wrong grade for almost every score, as in the 95 example above.
    • Separate ifs instead of a chain. Writing four independent if statements runs every test. Then a score of 95 would pass the 90, 80, 70 and 60 tests in turn, and the last assignment would win.
    • Out-of-range scores. The ladder has no upper check, so a score of 120 gets an A, and a negative score such as -5 gets an F. The lesson does not ask for validation.
    • Printing inside a branch. The print line belongs after the chain, so it appears once whatever the grade.

    Testing

    Change score and run the program. These scores, taken around every boundary, give these grades:

    scoreoutput
    100Grade: A
    90Grade: A
    89Grade: B
    80Grade: B
    79Grade: C
    70Grade: C
    69Grade: D
    60Grade: D
    59Grade: F
    0Grade: F

    The next version builds on this one.

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