Start learning free

LESSON 203 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in Kotlin: if / else if / else Letter Grades

Practice Read the full explanation

Use if/else if/else to turn a numeric score into a letter grade.

You start with:

val score = 85

var grade = ""

Build:

Grade: B

Rules:

  • score >= 90 is "A"; 80-89 is "B"; 70-79 is "C"; 60-69 is "D"; below 60 is "F".
  • The boundaries count: a score of exactly 90 is an A, exactly 80 a B, exactly 70 a C, exactly 60 a D, and 59 is an F.
  • Print "Grade: " followed by the letter grade, for example println("Grade: $grade")

grade is already declared in the starter code. Assign to it in an if/else if chain.

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")

STEP 1 OF 6
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in Kotlin, explained

    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

    1. Open the chain with the top grade. if (score >= 90), and inside the braces assign "A" to grade. Close the block with } on the line where the next branch continues.
    2. Add else if (score >= 80). It goes right after the closing brace of the previous block. It assigns "B".
    3. Add else if (score >= 70). It assigns "C".
    4. Add else if (score >= 60). It assigns "D".
    5. Finish with a plain else. It has no condition and assigns "F". Close the whole chain with its last }.
    6. Print after the chain. The println goes outside every brace, so it runs once, after a branch has set grade.

    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:

    • var for a value that changes. grade starts as an empty string and is reassigned by one of the branches, so it must be var. A val can'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.
    • if is also an expression. Kotlin lets you write val grade = if (score >= 90) "A" else if (score >= 80) "B" else "F", and the chosen value becomes the result. The lesson uses statements with a var because 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 of grade after the label.

    Following the chain on the lesson's data

    With score equal to 85, the program takes these steps:

    1. The first test asks whether 85 is at least 90. It is not, so the block that assigns "A" is skipped.
    2. The second test asks whether 85 is at least 80. It is, so grade becomes "B".
    3. The chain ends there. The tests for 70 and 60 are never evaluated, and neither is the else.
    4. 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 println is inside the last block, only failing scores print anything.
    • Forgetting the else. Without it, scores below 60 leave grade as the empty string, so the line prints Grade: with nothing after it.
    • A separate if for every grade. Several independent if statements all run, so a score of 95 passes every test and ends up with the last matching assignment, a D. The else keywords are what make the branches exclusive.

    Testing

    Change score and run again. The boundary values are the ones worth trying:

    scoreOutput
    100Grade: A
    90Grade: A
    89Grade: B
    80Grade: B
    79Grade: C
    70Grade: C
    69Grade: D
    60Grade: D
    59Grade: F
    0Grade: F
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free