What the exercise asks
The lesson starts with three grades stored in int variables, one each for math, English and science. You work out the average of the three, then print Passed the test if the average is 5 or more, and Failed the test otherwise. It pulls together variables, arithmetic, comparison operators and if-else.
The arithmetic: why 3.0 and not 3
The average is the sum divided by the count. The sum of the grades is 7 + 4 + 9 = 20, and dividing by three should give about 6.67. What C# gives you depends on the types:
Console.WriteLine((7 + 4 + 9) / 3); // 6
Console.WriteLine((7 + 4 + 9) / 3.0); // 6.666666666666667
When both operands are int, C# performs integer division and discards the fractional part, so 20 / 3 is 6. As soon as one operand is a double, as 3.0 is, the other is converted to double and the division keeps its decimals. With integer division the stored average would be a whole number, and the decimals you might want to show or compare against later would be gone.
The second detail is the parentheses. Division binds tighter than addition, so mathGrade + englishGrade + scienceGrade / 3.0 would divide only the last grade and then add the other two. You want the whole sum divided, so wrap the sum in parentheses.
Step by step
- Compute the average. Declare
double gradeand assign it the parenthesised sum divided by3.0. Storing it in adoublekeeps the fractional part, and naming itgrademakes the next lines easy to read. - Write the condition. Use
if (grade >= 5). A grade of exactly 5 passes, which is why the operator is>=and not>. - Print the pass. Inside the block,
Console.WriteLine("Passed the test");. - Print the fail. Add an
elseblock that prints"Failed the test". It runs for every average below 5.
The complete code
int mathGrade = 7;
int englishGrade = 4;
int scienceGrade = 9;
double grade = (mathGrade + englishGrade + scienceGrade) / 3.0;
if (grade >= 5) {
Console.WriteLine("Passed the test");
} else {
Console.WriteLine("Failed the test");
}
The average is about 6.67, so grade >= 5 is true and the program prints:
Passed the test
How the program flows
The three grade variables are only ever read. The average is computed once, stored in grade, and the if reads that stored value. Giving the average its own variable, instead of writing the long expression inside the condition, keeps the condition short and lets you print or reuse the value. For instance, Console.WriteLine(grade); would show 6.666666666666667.
Common mistakes
- Dividing by
3. The code still compiles and runs, but the average loses its decimals, so 6.67 becomes 6. With a pass mark of exactly 5 the pass or fail answer happens to stay the same, which is why this bug can hide until the rule changes, for example to 5.5. - Missing parentheses. Without them, only the science grade is divided. With these grades the result would be
7 + 4 + 3.0 = 14, a very different number. - Declaring the average as
int.int grade = (...) / 3.0;does not compile, because C# will not silently narrow adoubleto anint. Declare it asdouble, or add an explicit cast and accept losing the decimals. - Using
>instead of>=. An average of exactly 5 would fail. Check the boundary.
Trying other grades
Edit the three starting numbers in the editor to check the boundary. Grades of 5, 5 and 5 average exactly 5.0, so the program prints Passed the test. Grades of 4, 4 and 5 average about 4.33, so it prints Failed the test. Because the average is computed from the variables, you never change the rest of the code when the grades change.
Key takeaways
- Dividing two
intvalues in C# truncates; make one side adoubleto keep decimals. - Parenthesise a sum before dividing it.
- The condition of an
ifcan use adoublevariable just like anint. - Choose
>or>=by asking whether the boundary value should pass.