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
- Open the chain with
if (score >= 90)and assign"A"togradeinside the braces. Close the block with its}. - Continue with
else if (score >= 80)for the B. Theelsegoes 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. - Add
else if (score >= 70)for the C. Only scores below 80 get this far. - Add
else if (score >= 60)for the D. This is the last passing range. - Finish with
else, assign"F", and close the whole chain with its final}. - 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 anelse, all five paths assigngrade, so the print line compiles. If you leave the finalelseout, the compiler reportserror CS0165: Use of unassigned local variable 'grade', because a score below 60 would leavegradewithout a value. else ifis not a separate keyword. It is anelsewhose body is anotherifstatement. 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
boolexpressions. C# does not treat numbers as true or false, soif (score)would not compile. Always write the comparison.
Edge cases and common mistakes
- Boundaries. Use
>=, not>. Withscore > 90a 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 independentifstatements 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:
| score | output |
|---|---|
| 100 | Grade: A |
| 90 | Grade: A |
| 89 | Grade: B |
| 80 | Grade: B |
| 79 | Grade: C |
| 70 | Grade: C |
| 69 | Grade: D |
| 60 | Grade: D |
| 59 | Grade: F |
| 0 | Grade: F |
The next version builds on this one.
