Start learning free

LESSON 69 OF 347 · CONDITIONAL STATEMENTS

Student Passed in C#: Averages, Doubles 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

    C# · CONDITIONAL STATEMENTS

    Student Passed in C#, explained

    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

    1. Compute the average. Declare double grade and assign it the parenthesised sum divided by 3.0. Storing it in a double keeps the fractional part, and naming it grade makes the next lines easy to read.
    2. Write the condition. Use if (grade >= 5). A grade of exactly 5 passes, which is why the operator is >= and not >.
    3. Print the pass. Inside the block, Console.WriteLine("Passed the test");.
    4. Print the fail. Add an else block 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 a double to an int. Declare it as double, 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 int values in C# truncates; make one side a double to keep decimals.
    • Parenthesise a sum before dividing it.
    • The condition of an if can use a double variable just like an int.
    • Choose > or >= by asking whether the boundary value should pass.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free