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 trial

Ruby

Marwan Hassama
Marwan Hassama
865 Points

Why does 'elsif' have to come before an 'else' statement? Thanks

Why does 'elsif' have to come before an 'else' statement?

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are creating a set of mutually exclusive routes for your program logic to follow, meaning when an if block is entered, there is only one way out. the code is executing from top to bottom, so the first acceptable way out will be taken. so if your program logic is sorting numbers for example, and the current number is 500, and you have bins of less than 20, 20-70, or greater than 70, the first test (if) would be: is the number less than 20? 500 > 20, so no, next, or else if number 70 or less? no also, next, or else, meaning, the number isn't any previous choice, greater than 70. it's the default choice if nothing previous was taken.