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
- Declare
gradeas astringbefore the chain. Astringwithout 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. - Open the chain with
if (score >= 90)and assign"A"inside the block. - 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. - Add
else if (score >= 70)for "C". - Add
else if (score >= 60)for "D". - Finish with a plain
elsethat assigns "F", and close the chain. - 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:
| score | first true test | grade |
|---|---|---|
| 100 | score >= 90 | A |
| 89 | score >= 80 | B |
| 79 | score >= 70 | C |
| 60 | score >= 60 | D |
| 59 | none, so the else runs | F |
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
gradeinside a branch. It is gone after the closing brace, and the print then fails to compile. - A separate
iffor each range. Several independentifstatements all run their tests, so a score of 95 would be assigned A, then B, then C. Theelsekeeps 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.
