Start learning free

LESSON 203 OF 285 · PROJECTS: EVERYDAY PROGRAMS

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

Practice Read the full explanation

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

You start with:

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)

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 fmt.Println("Grade: " + grade)
STEP 1 OF 6
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in Go, explained

    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

    1. Start with if score >= 90 {. Inside the block, assign "A" to grade. Use plain =, because grade already exists. Close the block with } and keep the next branch on the same line.
    2. Add else if score >= 80 { that sets grade to "B". It starts on the line with the closing brace of the block before.
    3. Add else if score >= 70 { for "C".
    4. Add else if score >= 60 { for "D".
    5. Finish with a plain else { that sets "F", and close the whole chain with }.
    6. 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.
    • else must follow the closing brace on the same line. Go inserts a semicolon at the end of a line that finishes with }. If else starts on the next line, the compiler reports syntax 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 outer grade. The compiler stops with grade declared and not used, and even if the new variable were used, the outer grade would stay "". Use grade = "A".
    • Comparison gives a bool. score >= 90 is true or false, and Go has no truthy numbers. An if needs a real bool.
    • 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 >= 60 first, 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 if statements. A chain of independent ifs does not skip anything: a score of 95 would be tested against every condition, and the last one that is true wins. Use else if so 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:

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