Start learning free

LESSON 70 OF 348 · CONDITIONAL STATEMENTS

Student Passed in Kotlin: Average 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

    KOTLIN · CONDITIONAL STATEMENTS

    Student Passed in Kotlin, explained

    What the exercise asks

    This exercise combines several things you have already met: variables, arithmetic, a comparison and an if-else. You start with three variables holding a student's grades:

    val mathGrade = 7
    val englishGrade = 4
    val scienceGrade = 9
    

    The task is to compute the average of the three grades and print whether the student passed. A student passes with an average of 5 or more.

    Step 1: compute the average

    The average is the sum of the grades divided by how many there are. Declare a new val named grade for it:

    val grade = (mathGrade + englishGrade + scienceGrade) / 3.0
    

    Two details matter here.

    • The parentheses. Division happens before addition. Without them, mathGrade + englishGrade + scienceGrade / 3.0 divides only the science grade and then adds the other two. With the grades above that gives 14.0 instead of the real average.
    • The 3.0. Dividing an Int by another Int is integer division in Kotlin: it drops the fractional part. With grades 4, 4 and 5 the sum is 13, and 13 / 3 is 4, while 13 / 3.0 is 4.333333333333333. Dividing by 3.0, a Double, makes the whole result a Double, so the fraction is kept. Kotlin does not convert silently the other way. Writing val avg: Double = 13 / 3 is a compile error, because 13 / 3 is an Int.

    For the starting grades, 7 + 4 + 9 is 20, and 20 divided by 3.0 is 6.666666666666667.

    Step 2: check the average

    Write an if whose condition is grade >= 5. The >= operator is true when grade is 5 or more, so an average of exactly 5 passes. Comparing a Double with the whole number 5 is fine here: >= works across Double and Int, unlike ==, which would be rejected.

    Step 3: the passing branch

    Inside the if block, print "Passed the test" with println.

    Step 4: the failing branch

    Add an else block that prints "Failed the test". It needs no condition, because it runs whenever grade >= 5 is false.

    The full solution

    val mathGrade = 7
    val englishGrade = 4
    val scienceGrade = 9
    
    val grade = (mathGrade + englishGrade + scienceGrade) / 3.0
    
    if (grade >= 5) {
        println("Passed the test")
    } else {
        println("Failed the test")
    }
    

    The average is about 6.67, which is at least 5, so the program prints Passed the test. Change englishGrade and scienceGrade to 2 and the average falls below 5, so the output becomes Failed the test.

    Trying other grades

    Changing the three starting values shows how the boundary behaves:

    • Grades 5, 5 and 5 give an average of exactly 5.0, and the program prints Passed the test.
    • Grades 4, 5 and 5 give 4.666666666666667, which is below 5, so it prints Failed the test.
    • Grades 10, 1 and 4 also average 5.0. One very high grade can balance a very low one, because the average only looks at the total.

    Notice what the program is doing in each case. First it turns three separate values into one number. Then it asks a single yes-or-no question about that number. Finally it picks one of two messages. Most small programs have that shape: compute, decide, report.

    Common mistakes

    • Forgetting the parentheses. This is the most frequent error, and it produces a plausible-looking number, so nothing warns you.
    • Using > instead of >=. An average of exactly 5 would then fail. Check the boundary.
    • Using val when the value must change. Here every value is set once, so val is right. Only switch to var if you need to reassign.
    • Rounding by hand. Do not convert the average to an Int before comparing. Truncating 4.67 gives 4 and hides the real value, which matters as soon as a threshold is not a whole number.
    • Printing the grade by accident. The task asks for a message about passing, not for the number itself.

    Key takeaways

    • Use parentheses so the sum is computed before the division.
    • Divide by a Double such as 3.0 to keep the fractional part of an average.
    • A comparison produces a Boolean that decides which branch of if-else runs.
    • Test the boundary value, here an average of exactly 5.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free