Start learning free

LESSON 203 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in Scala: Letter Grade with if / else if

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(s"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(s"Grade: $grade")

STEP 1 OF 6
    Ctrl + Enter to check

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in Scala, explained

    Problem overview

    Version 3 turns a numeric score into a letter. The starter code has val score = 85 and an empty var grade = "". The program sets grade to "A" for 90 and above, "B" for 80 to 89, "C" for 70 to 79, "D" for 60 to 69 and "F" below 60. The boundaries belong to the higher grade: exactly 90 is an A and 59 is an F.

    For a score of 85 the output is:

    Grade: B
    

    Approach

    A single comparison can't give five answers, so the program needs a decision with several branches. Scala's if / else if / else is the tool for it.

    The five ranges don't overlap, so one chain of if, else if and else picks exactly one of them. A chain tests its conditions from top to bottom and runs the first block whose condition is true; all the later branches are skipped.

    That order is what keeps the conditions short. Start with the highest grade. When the program reaches the test for 80, it already knows the score is below 90, so score >= 80 alone means 80 to 89. Every condition needs only a lower limit, and the final else catches everything that is left, anything below 60.

    The ranges in the statement have two ends, such as 80 to 89, but the code never writes the upper end. Writing score >= 80 && score <= 89 would work too, yet it repeats a fact the chain already guarantees and gives a typo twice as many places to hide. A plain lower bound per branch is shorter and easier to check against the table of grades.

    Step by step

    1. Open with the A branch. if (score >= 90), and set grade to "A" inside the braces. Keep the closing brace on the same line as the next keyword, so the chain continues.
    2. Add the B branch as else if (score >= 80). It only runs for scores below 90.
    3. Add the C branch for 70 or more, which means 70 to 79 at this point.
    4. Add the D branch for 60 or more, the last passing range.
    5. Finish with else, which has no condition and sets "F". Close the whole chain with its brace.
    6. Print after the chain. The println line sits outside the braces, so it runs once with whichever grade was chosen.

    The solution in Scala

    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(s"Grade: $grade")
    

    Tracing a score of 85

    The program first asks whether 85 is at least 90. It is not, so the A block is skipped. The next test asks whether 85 is at least 80. That is true, so grade becomes "B", and the program jumps past the rest of the chain, skipping the tests for 70 and 60 and the else. The println then prints Grade: B. A score of 59 fails all four tests, so it reaches the else and prints Grade: F.

    Why var and val

    grade is a var because the chain assigns it after its creation, while score never changes and stays a val. Assigning to a val is a compile error, so the starter code's var is what lets the branches set the letter.

    The expression form

    In Scala, if is also an expression that returns a value, so the same logic can be written without a var:

    val grade = if (score >= 90) "A" else if (score >= 80) "B" else if (score >= 70) "C" else if (score >= 60) "D" else "F"
    

    Looped over the scores 85, 90 and 59, this one-line version gives B, A and F, the same as the chain. The lesson uses the assignment form, with braces and one branch per line, because it makes each branch's job explicit.

    Testing the boundaries

    The boundaries are where this program usually breaks. Running the same chain on different scores gives:

    scoreprinted
    100Grade: A
    90Grade: A
    89Grade: B
    80Grade: B
    79Grade: C
    70Grade: C
    69Grade: D
    60Grade: D
    59Grade: F
    0Grade: F

    Edge cases and common mistakes

    • Writing > instead of >=. Then exactly 90 would fall through to a B.
    • Testing from the bottom. If the first test is score >= 60, every passing score matches it and gets a D. A chain that starts if (score >= 60) "D" else if (score >= 70) "C" else "F" gives D for both 85 and 75.
    • Breaking the chain. Starting a new if for each range, instead of else if, makes every test run, and later matches overwrite earlier ones. With four separate tests of this kind (90, 80, 70 and 60) and a score of 85, the tests for 80, 70 and 60 all pass in turn, the last assignment wins, and the program prints Grade: D instead of Grade: B. The word else is what makes the branches exclusive.
    • Printing inside a branch. Put println after the chain once, not in each branch.
    • Scores outside 0 to 100. The chain does not check them: 150 gives A and -5 gives F.

    Key takeaways

    • An if / else if / else chain runs only the first branch whose test is true.
    • Order the tests from the highest range down, so each test needs just one bound.
    • Check each boundary value, because off-by-one errors hide there.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free