Start learning free

LESSON 204 OF 292 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in Swift: 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:

let 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 print("Grade: \(grade)")

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

let 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"
}
print("Grade: \(grade)")

STEP 1 OF 6
    Ctrl + Enter to check

    SWIFT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in Swift, explained

    Problem overview

    The program holds a numeric score and a var grade that starts as empty text. It must turn the score into a letter and print it after 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, so exactly 90 is an A and 59 is an F.

    With score = 85 the program prints Grade: B. Earlier versions computed numbers; this one makes a decision, which is the job of if.

    Approach: a ladder from the top down

    Five ranges, five outcomes. A chain of if, else if and else tests the conditions in order and runs only the first block whose condition is true. Everything after that block is skipped.

    That order is the whole idea. If you start from the highest grade, a branch is reached only when all the higher tests have failed. When the >= 80 test runs, the score is already known to be below 90, so you don't need to write score >= 80 && score < 90. Each test needs only a lower bound, and the final else needs no test at all: it takes whatever is left, which is everything below 60.

    Step by step

    1. Open with the top grade. if score >= 90 sets grade to "A". Close the block with } on the same line where the next branch begins.
    2. Add else if score >= 80 for "B". It only runs when the first test failed, so it covers 80 to 89.
    3. Add else if score >= 70 for "C", covering 70 to 79.
    4. Add else if score >= 60 for "D", covering 60 to 69.
    5. Finish with a plain else that sets "F", and close the chain.
    6. Print after the chain. The print line sits outside all the braces, so it runs once, after a branch has chosen the grade.

    The solution in Swift

    let 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"
    }
    print("Grade: \(grade)")
    

    Swift specifics:

    • No parentheses around the condition. Swift writes if score >= 90 {. Parentheses are allowed but unusual.
    • Braces are mandatory. Even a one-line branch needs { }, so there is no dangling-statement trap.
    • The condition must be a Bool. if score { is not allowed for an integer; you always write a comparison.
    • let and var. score never changes, so it is a let. grade is assigned later, so it is a var. Starting it as "" gives it the type String, and the chain then replaces the empty text with a letter.

    Following the chain with score = 85

    The program first asks whether 85 is at least 90. It is not, so the block for "A" is skipped and the next condition is tried. Is 85 at least 80? Yes, so grade becomes "B". The remaining conditions, 70 and 60, are never evaluated, and neither is the final else. Control jumps past the end of the chain and reaches the print line, which shows Grade: B.

    Now change the score to 55. The tests for 90, 80, 70 and 60 all fail one after another, and the else block runs, setting "F". In every case exactly one block runs, because the chain ends with an else that catches the rest. That guarantee is why grade never keeps its starting empty text.

    The same idea works for any set of ranges: order the conditions from the most restrictive to the least, and let each branch rely on the ones before it.

    Edge cases and common mistakes

    • Using > instead of >=. With > 90, a score of exactly 90 falls through to the B branch. Every boundary in the statement is inclusive.
    • Testing the lowest range first. If the chain starts with score >= 60, a score of 95 matches immediately and the program prints D. The ladder only works from the top down.
    • Using separate if statements. Five independent if blocks would all run for a high score, and the last one that matches would win. else if keeps the choices exclusive.
    • Printing inside a branch. Put the print after the chain, once. Copying it into each branch works, but it repeats the line five times and is easy to get wrong when you change the format.
    • Declaring grade with let. With let grade = "", every later assignment in the chain is a compile error, because a constant that already has a value can't be changed. The starting code uses var for that reason.
    • Scores with decimals. score is an Int here, so a value like 89.5 can't be stored in it. Handling such scores would need a Double and a decision about where 89.5 belongs; this lesson avoids the question.

    Testing

    Each result comes from running the same chain with a different score.

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

    The pairs on each side of a boundary (90 and 89, 80 and 79, and so on) are the ones that catch off-by-one mistakes. The next version builds on this ladder.

    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free