Start learning free

LESSON 206 OF 293 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in Rust: 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:

// Rust: the editor starts empty - declare the data yourself, for example:
let 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 println!("Grade: {}", grade);

Declare let mut grade = ""; first, then assign in an if/else if chain.

let score = 85;
let mut 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";
}
println!("Grade: {}", grade);

STEP 1 OF 8
    Ctrl + Enter to check

    RUST · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in Rust, explained

    Problem overview

    The program turns a numeric score into a letter grade and prints it as Grade: B. The scale is: 90 or more is an A, 80 to 89 a B, 70 to 79 a C, 60 to 69 a D, and anything below 60 an F. The boundaries belong to the higher grade: exactly 90 is an A, exactly 80 a B, and 59 is an F.

    The lesson starts with score = 85, which falls in the 80 to 89 range, so the output is Grade: B.

    The previous version computed a weighted score with arithmetic. This one makes a decision based on a score.

    Approach: check from the top down

    A chain of if, else if and else tests its conditions in order and runs only the first block whose condition is true. That makes the order the whole design.

    Start with the highest grade. The first test, score >= 90, catches every A. A score that reaches the second test is known to be below 90, so score >= 80 only needs a lower bound to mean "80 to 89". The same reasoning applies to 70 and 60. The final else has no condition: whatever is left is below 60, so it is an F.

    Checking from the bottom would break this. With score >= 60 first, a 95 would match immediately and get a D.

    The closing else is what guarantees an answer. If it were missing, a score below 60 would match no branch, grade would keep its starting empty text, and the program would print Grade: with nothing after it. Starting grade as an empty string gives the variable a valid value from the beginning, so the compiler accepts the print even before it knows which branch runs.

    Here is how the lesson's data moves through the chain. With score = 85, the first test fails because 85 is below 90. The second test, score >= 80, is true, so grade becomes "B". The remaining tests are skipped, and the print runs next.

    These are the results for the values on both sides of each boundary:

    ScoreGrade
    100A
    90A
    89B
    80B
    79C
    70C
    69D
    60D
    59F
    0F

    Step by step

    1. Declare score with let, set to 85.
    2. Declare grade with let mut, starting as empty text. The branches assign to it later, which is why it needs mut.
    3. Open the chain with an if for scores of at least 90. Inside, set grade to "A".
    4. Add an else if for 80 or more, setting grade to "B". It continues on the same line as the closing brace of the previous block.
    5. Add an else if for 70 or more, setting "C".
    6. Add an else if for 60 or more, setting "D".
    7. Finish with a plain else that sets "F", and close the whole chain.
    8. Print after the chain, outside the braces, so the line runs once with whichever grade the chain picked.

    The solution in Rust

    let score = 85;
    let mut 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";
    }
    println!("Grade: {}", grade);
    

    Rust details worth knowing:

    • mut is required. Without it, assigning grade = "A" to a variable that already holds "" is a compile error, because a let binding can't be changed.
    • The type is &str. A string literal such as "A" is a string slice, and let mut grade = "" makes grade a &str. Each branch assigns another literal of the same type.
    • No parentheses around the condition. Rust writes if score >= 90 {. The braces are mandatory, even for a single statement.
    • A compiler warning. The starting "" is never read, because every branch overwrites it before the print, so rustc warns that the value assigned to grade is never read. It is only a warning, and the program runs normally.
    • if is also an expression. The same logic can be written as let grade = if score >= 90 { "A" } else if score >= 80 { "B" } else { "F" };, where each branch gives a value and no mut is needed. This lesson uses the assigning style.

    Complexity

    The chain makes at most four comparisons, whatever the score, so it takes constant time and constant space.

    Edge cases and common mistakes

    • The boundaries. Scores 90, 80, 70 and 60 give A, B, C and D. Writing score > 90 would turn a 90 into a B.
    • Just below a boundary. 89 gives B, 79 gives C, 69 gives D and 59 gives F.
    • The extremes. 100 gives A and 0 gives F.
    • Testing from the bottom. A chain that starts with score >= 60 gives a D to a score of 95.
    • Separate ifs instead of else if. With independent if statements, every true condition runs, so a 95 would set grade to A, then B, C and D in turn, and end up as D. The else keeps the branches exclusive.

    Testing

    • Score 85 prints Grade: B.
    • Scores 100 and 90 print Grade: A.
    • Scores 89 and 80 print Grade: B.
    • Scores 79 and 70 print Grade: C.
    • Scores 69 and 60 print Grade: D.
    • Scores 59 and 0 print Grade: F.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free