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:
| Score | Grade |
|---|---|
| 100 | A |
| 90 | A |
| 89 | B |
| 80 | B |
| 79 | C |
| 70 | C |
| 69 | D |
| 60 | D |
| 59 | F |
| 0 | F |
Step by step
- Declare
scorewithlet, set to 85. - Declare
gradewithlet mut, starting as empty text. The branches assign to it later, which is why it needsmut. - Open the chain with an
iffor scores of at least 90. Inside, setgradeto"A". - Add an
else iffor 80 or more, settinggradeto"B". It continues on the same line as the closing brace of the previous block. - Add an
else iffor 70 or more, setting"C". - Add an
else iffor 60 or more, setting"D". - Finish with a plain
elsethat sets"F", and close the whole chain. - 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:
mutis required. Without it, assigninggrade = "A"to a variable that already holds""is a compile error, because aletbinding can't be changed.- The type is
&str. A string literal such as"A"is a string slice, andlet mut grade = ""makesgradea&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 togradeis never read. It is only a warning, and the program runs normally. ifis also an expression. The same logic can be written aslet grade = if score >= 90 { "A" } else if score >= 80 { "B" } else { "F" };, where each branch gives a value and nomutis 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 > 90would 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 >= 60gives a D to a score of 95. - Separate
ifs instead ofelse if. With independentifstatements, every true condition runs, so a 95 would setgradeto A, then B, C and D in turn, and end up as D. Theelsekeeps 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.
