K .2,356 Points
Why does it matter what order we put the 'if' and 'elsif' statements?
I have this code the error is saying it checks if speed is greater than 60 but doesn't print out "too fast" . why is that?
def check_speed(speed) if speed < 45 puts "too slow" elsif speed >= 45 puts "speed OK" elsif speed > 60 puts "too fast" end end
i know that if i rearrange the statements it works.. i just dont understand why.
def check_speed(speed) if speed > 60 puts "too fast" elsif speed >= 45 puts "speed OK" elsif speed < 45 puts "too slow" end end
KRIS NIKOLAISEN54,281 Points
The conditions are checked in order and the first one that is true executes. In your case:
elsif speed >= 45
meets the condition for speeds greater than 60 so that is what executes