Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialK .
2,357 PointsWhy 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
1 Answer
KRIS NIKOLAISEN
54,972 PointsThe 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