Start learning free

LESSON 72 OF 415 · CONDITIONAL STATEMENTS

Student Passed in Swift: Averages 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

    SWIFT · CONDITIONAL STATEMENTS

    Student Passed in Swift, explained

    The exercise

    The starting code stores a student's grades in three constants:

    let mathGrade = 7
    let englishGrade = 4
    let scienceGrade = 9
    

    The task is to compute the average of the three grades and print whether the student passed. A pass is an average of 5 or more, and the program prints Passed the test or Failed the test. This combines three ideas from the chapter: constants, arithmetic and an if-else statement.

    Why the average needs a Double

    The three grades are Int values, because they are whole-number literals. The sum is 20, and 20 divided by 3 is 6.666..., so the average can have a fractional part.

    Dividing integers in Swift gives an integer. The remainder is thrown away, so 20 / 3 is 6, not 6.67. For a threshold of exactly 5 the pass or fail answer would come out the same, but the average itself would be wrong, and any other threshold would misbehave: a sum of 14 gives an integer average of 4, which fails a pass mark of 4.5, while the true average, 4.67, passes it.

    The fix is to do the division with Double values. Swift never converts between number types on its own, so mixing them is an error. With let m = 7 and let g = 2.5, the expression m + g fails with binary operator '+' cannot be applied to operands of type 'Int' and 'Double'. You have to convert explicitly, with Double(...).

    Step by step

    1. Declare the constant grade with let. The average is never changed after it is computed, so a constant is the right choice.
    2. Convert the sum. Add the three grades inside parentheses, then wrap the sum in Double(...). The conversion must be applied to the whole sum, before the division.
    3. Divide by 3.0. A Double divided by a Double literal is a Double division, and Swift infers the type of grade as Double. Writing 3 would work too, because an integer literal can be read as a Double when the other side is one, but 3.0 makes the intent obvious.
    4. Write the if. The condition is grade >= 5. Comparing a Double with the literal 5 is fine, because the literal is read as a Double.
    5. Print the pass message inside the if block.
    6. Add the else. It prints the failure message and needs no condition, since anything below 5 is a failure.
    let mathGrade = 7
    let englishGrade = 4
    let scienceGrade = 9
    
    let grade = Double(mathGrade + englishGrade + scienceGrade) / 3.0
    
    if grade >= 5 {
        print("Passed the test")
    } else {
        print("Failed the test")
    }
    

    The sum is 20 and grade is 6.666666666666667, which is at least 5, so the program prints Passed the test.

    Where the conversion goes

    The placement of Double(...) changes the answer. With the grades above:

    let sum = mathGrade + englishGrade + scienceGrade
    print(sum / 3)
    print(Double(sum) / 3.0)
    print(Double(sum / 3))
    
    6
    6.666666666666667
    6.0
    

    The first line is an integer division. The second converts first and then divides, which is what the exercise needs. The third divides as integers and only then converts, so the fraction is already lost and the result is just 6.0. Writing let g: Double = sum / 3.0 is an error, because sum is still an Int: the compiler says cannot convert value of type 'Int' to expected argument type 'Double'.

    Common mistakes

    • Dividing as integers. The result loses its fraction before the comparison with 5.
    • Forgetting the parentheses around the sum. Double(mathGrade) + englishGrade + scienceGrade mixes types again and does not compile.
    • Using > instead of >=. An average of exactly 5 is a pass, and > 5 would fail it.
    • Using var for everything. It works, but let tells the reader the value stays fixed.

    Testing

    Change the three constants and rerun the program:

    • Grades 7, 4 and 9 average 6.67 and print Passed the test.
    • Grades 4, 5 and 5 average 4.67 and print Failed the test.
    • Grades 5, 5 and 5 average exactly 5 and print Passed the test.

    Key takeaways

    • Integer division drops the remainder, and Swift will not mix Int and Double in one expression.
    • Convert with Double(...) before dividing, not after.
    • Compute the value once in a let, then use it in the condition.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free