Variables Review

So I just reached the end of the variables portion of swift and it gives me an option to do an optional task. I am a bit confused to the solution. See my side note.

var height: Double = 1.85
var weight: Double = 82
var bmi: Double <-------- Why is the var bmi given Double

bmi = weight / (height * height)

print(“Your BMI is (bmi)”)

This is initialising the variable, but not assigning anything to it. That means the variable, while it exists, doesn’t hold any value (or it holds the default Swift value). bmi needs to be a Double, because you want it to be able to hold decimal places. For instance, if height was 3, and weight was 10, then you’d get bmi = 10/9 = 1.111 (recurring). If bmi was just an integer, then you’d lose accuracy.

I hope this helps!

That does help thank you. When I first saw it it was super confusing to me.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.