Problem overview
This version turns a numeric score into a letter grade. A score of 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, and 59 is an F. The starter code declares the score and an empty grade:
val score = 85
var grade = ""
A score of 85 is at least 80 but below 90, so the program prints:
Grade: B
Approach
The program has to choose one of five outcomes, and the choices are ordered. The natural tool is an if / else if / else chain. Kotlin checks the conditions from top to bottom and runs only the first branch whose condition is true. Later conditions aren't evaluated at all.
That order lets each test be short. Start from the highest grade. Once score >= 90 has failed, the score is known to be below 90, so the next test only needs a lower bound: score >= 80 already means 80 to 89. The same reasoning applies to each following branch, and the final else handles everything left, which is every score under 60.
The chain only works from the top down. A chain that tests score >= 60 first would give a D to a score of 95, because 95 is at least 60 and the chain stops at the first match.
Step by step
- Open the chain with the top grade.
if (score >= 90), and inside the braces assign"A"tograde. Close the block with}on the line where the next branch continues. - Add
else if (score >= 80). It goes right after the closing brace of the previous block. It assigns"B". - Add
else if (score >= 70). It assigns"C". - Add
else if (score >= 60). It assigns"D". - Finish with a plain
else. It has no condition and assigns"F". Close the whole chain with its last}. - Print after the chain. The
printlngoes outside every brace, so it runs once, after a branch has setgrade.
The solution in Kotlin
val score = 85
var 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")
Kotlin details worth knowing:
varfor a value that changes.gradestarts as an empty string and is reassigned by one of the branches, so it must bevar. Avalcan't be assigned a second time.- Parentheses and braces. The test goes in parentheses after
if. A branch with a single statement may skip the braces, but braces are the clearer style, and the lesson uses them. - Exactly one branch runs. Because the chain ends with
else, every score reaches some assignment, including negative scores and scores above 100. A score of 150 gets an A, since nothing in the chain limits the top. ifis also an expression. Kotlin lets you writeval grade = if (score >= 90) "A" else if (score >= 80) "B" else "F", and the chosen value becomes the result. The lesson uses statements with avarbecause that is the form you need while the branches do more than yield a value.- A string template prints the letter.
"Grade: $grade"inserts the current value ofgradeafter the label.
Following the chain on the lesson's data
With score equal to 85, the program takes these steps:
- The first test asks whether 85 is at least 90. It is not, so the block that assigns
"A"is skipped. - The second test asks whether 85 is at least 80. It is, so
gradebecomes"B". - The chain ends there. The tests for 70 and 60 are never evaluated, and neither is the
else. - The last line prints
Grade: B.
With a score of 59 the story is different. All four conditions fail one after another, and the else block runs and assigns "F". So a score that falls through every test is exactly what the final else is for.
Edge cases and common mistakes
- The boundaries. Use
>=, not>. With>, a score of exactly 90 would fail the first test and land in the B branch. - Reversed order. Testing the lowest bound first makes every passing score a D: 85 and 95 both print D, because 60 is the first test that succeeds.
- Printing inside a branch. If the
printlnis inside the last block, only failing scores print anything. - Forgetting the
else. Without it, scores below 60 leavegradeas the empty string, so the line printsGrade:with nothing after it. - A separate
iffor every grade. Several independentifstatements all run, so a score of 95 passes every test and ends up with the last matching assignment, a D. Theelsekeywords are what make the branches exclusive.
Testing
Change score and run again. The boundary values are the ones worth trying:
| 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 |
