Problem overview
Version 3 of Report Card turns a numeric score into a letter grade. The starting code has a score and an empty grade string:
score := 85
grade := ""
The scale is: 90 and above 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: exactly 90 is an A, and 59 is an F. The program prints the result, and for a score of 85 the output is:
Grade: B
Approach: a ladder from the top down
A score falls into exactly one range, so the program needs a chain of conditions in which only one branch runs. Go's if / else if / else does that: it tests the conditions in order, runs the block of the first one that is true and skips the rest.
Because the tests run in order, you can start with the highest range and give each later test only a lower bound. If score >= 90 was false, the score is already below 90, so score >= 80 really means 80 to 89. Each later test inherits that knowledge, and there is no need to write score >= 80 && score < 90. The final else has no condition and takes whatever is left, which is everything below 60.
Tracing the starting score
With score equal to 85, the first test, score >= 90, is false, so Go moves on to the next one. The second test, score >= 80, is true, so grade becomes "B". The chain ends there: the tests for 70 and 60, and the else, are never evaluated. For a score of 59, every test is false and the else block runs. The order of the tests is therefore part of the logic, not just a style choice.
Step by step
- Start with
if score >= 90 {. Inside the block, assign"A"tograde. Use plain=, becausegradealready exists. Close the block with}and keep the next branch on the same line. - Add
else if score >= 80 {that setsgradeto"B". It starts on the line with the closing brace of the block before. - Add
else if score >= 70 {for"C". - Add
else if score >= 60 {for"D". - Finish with a plain
else {that sets"F", and close the whole chain with}. - Print after the chain, outside all the braces, so that the line runs once, after a grade has been chosen. The grade is already a string, so it can be joined to
"Grade: "with+directly.
The solution in Go
score := 85
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"
}
fmt.Println("Grade: " + grade)
The code uses the fmt package. The language details worth knowing:
- No parentheses, braces required. Go's conditions are written without parentheses, and the braces are mandatory even for a one-line body.
elsemust follow the closing brace on the same line. Go inserts a semicolon at the end of a line that finishes with}. Ifelsestarts on the next line, the compiler reportssyntax error: unexpected else, expected }. That is why the lesson says to continue the chain on the line of the brace.- Assign, don't redeclare. Writing
grade := "A"inside a branch would create a new variable that exists only inside the braces and hide the outergrade. The compiler stops withgrade declared and not used, and even if the new variable were used, the outergradewould stay"". Usegrade = "A". - Comparison gives a
bool.score >= 90istrueorfalse, and Go has no truthy numbers. Anifneeds a realbool. - Comparison of integers is exact. The boundary checks use
>=, so 90 is an A. With>the score 90 would fall to a B.
Edge cases and common mistakes
- Wrong order. If the ladder starts from the bottom, with
score >= 60first, every passing score matches the first branch. A score of 95 gets a D, because that first test is already true and the rest are skipped. - Using
>instead of>=. A score of exactly 90, 80, 70 or 60 would drop to the grade below. - Separate
ifstatements. A chain of independentifs does not skip anything: a score of 95 would be tested against every condition, and the last one that is true wins. Useelse ifso only one branch runs. - Printing inside a branch. The print belongs after the chain. Inside one branch it would only print for that grade.
- Scores out of range. The ladder has no upper limit and no lower one, so 100 is an A and a negative score such as -5 is an F.
Testing
Run these scores through the ladder, and pay special attention to the boundaries:
| Score | Grade |
|---|---|
| 100 | A |
| 90 | A |
| 89 | B |
| 80 | B |
| 79 | C |
| 70 | C |
| 69 | D |
| 60 | D |
| 59 | F |
| 0 | F |
