Start learning free

LESSON 71 OF 338 · CONDITIONAL STATEMENTS

Student Passed in Go: Averages, Conversions and if/else

Lesson Read the full explanation

We created three variables to store the grades obtained by a student at math, english and science.

You need to compute the average of the grades and print to the console if the user has passed or not.


Assignment
Follow the Coding Tutorial and let's practice with everything we've learned!


STEP 1 OF 4
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    Student Passed in Go, explained

    What the exercise asks for

    This exercise combines several things from the chapter. The starter code stores three grades in variables:

    mathGrade := 7
    englishGrade := 4
    scienceGrade := 9
    

    The task is to compute the average of the three grades and print whether the student passed. An average of 5 or more is a pass. Here the sum is 7 + 4 + 9 = 20, and 20 divided by 3 is about 6.67, so the student passed and the program prints Passed the test.

    Why the division needs care

    All three grades are int values, and in Go, dividing an int by an int gives an int: the fractional part is thrown away. Compare the two results:

    fmt.Println((mathGrade + englishGrade + scienceGrade) / 3)
    m, e, s := 5, 5, 4
    fmt.Println((m+e+s)/3, float64(m+e+s)/3.0)
    

    The first line prints 6, not 6.67. The second prints 4 4.666666666666667: the same average, cut down to 4 in integer arithmetic and exact once it is computed as a float64. The stored average is wrong in the integer version, and any rule that depends on the fraction, such as a pass mark of 4.5, would go wrong with it. So the average has to be computed in floating point.

    Go also never converts between numeric types on its own. This code does not compile:

    a := 7
    var f float64 = 2.5
    fmt.Println(a + f)
    

    The compiler reports invalid operation: a + f (mismatched types int and float64). You convert explicitly, writing float64(a), and then the two sides match.

    Step by step

    1. Declare grade with :=. Add the three grades together, wrap the sum in parentheses, and convert it with float64( ... ). Then divide by 3.0. The result is a float64, so grade gets that type.
    2. Write the if condition. A grade of 5 or more passes, so the test is grade >= 5. The >= includes 5 itself.
    3. Print the pass message inside the block: fmt.Println("Passed the test").
    4. Add the else block on the same line as the closing brace and print "Failed the test".

    Why the parentheses matter

    Division binds tighter than addition, just like in arithmetic. Without parentheses, only the last grade is divided:

    fmt.Println(mathGrade + englishGrade + scienceGrade / 3)
    

    This prints 14, because it computes 7 + 4 + (9 / 3). Parentheses make the sum happen first.

    Where the conversion goes

    The conversion is applied to the sum, float64(mathGrade + englishGrade + scienceGrade), and then the division by 3.0 is a float division. Converting after the division, as in float64((mathGrade + englishGrade + scienceGrade) / 3), would be too late: the integer division has already dropped the fraction, and the result is just 6 turned into 6.0.

    The complete solution

    mathGrade := 7
    englishGrade := 4
    scienceGrade := 9
    
    grade := float64(mathGrade + englishGrade + scienceGrade) / 3.0
    
    if grade >= 5 {
        fmt.Println("Passed the test")
    } else {
        fmt.Println("Failed the test")
    }
    

    With the starter values it prints Passed the test. If you also want to see the average itself, fmt.Println(grade) prints 6.666666666666667, and fmt.Printf("%.2f\n", grade) prints 6.67.

    Trying other grades

    Changing the three starter values is the quickest way to test all the branches. With the solution above:

    • 5, 5, 5 gives an average of exactly 5 and prints Passed the test. This is the boundary, and it is why the comparison is >=.
    • 5, 5, 4 gives about 4.67 and prints Failed the test.
    • 10, 0, 5 gives exactly 5 and prints Passed the test: one strong grade can make up for a weak one, since an average only looks at the total.
    • 1, 1, 1 gives 1 and prints Failed the test.

    Common mistakes

    • Dividing integers. The average silently loses its fractional part.
    • Leaving out the parentheses around the sum.
    • Mixing int and float64 in one expression, which is a compile error.
    • Using > instead of >=. An average of exactly 5 must pass.
    • Declaring a variable and not using it. Go rejects unused local variables, so every grade variable must appear in the calculation.

    Key takeaways

    • Integer division in Go truncates; convert to float64 before dividing when you need the fraction.
    • Go never mixes numeric types implicitly. Convert with float64(x).
    • Parentheses control the order of operations.
    • A comparison like grade >= 5 feeds directly into if/else.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free