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
- Declare
gradewith:=. Add the three grades together, wrap the sum in parentheses, and convert it withfloat64( ... ). Then divide by3.0. The result is afloat64, sogradegets that type. - Write the
ifcondition. A grade of 5 or more passes, so the test isgrade >= 5. The>=includes 5 itself. - Print the pass message inside the block:
fmt.Println("Passed the test"). - Add the
elseblock 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
intandfloat64in 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
float64before 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 >= 5feeds directly intoif/else.