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

General Discussion

when would you want to use a switch statement instead of an if else statement?

I'm learning about switch statements and I would like to know is there really a big difference between switch statements and if else statements.........

2 Answers

else is normally dessicated with an if statement like a pair :-)

When you have multiple options, and it can only be one outcome.

Sure you could write an if, elsif, elsif, elsif, elsif, else statement, but that becomes long and drawn out. A switch is nice because you can give each statement a case to check against, execute if that check "checks out", and then break out of the switch.

My humble opinion, if you have to do any more than a if, elsif, else statement, you probably should have used a switch statement.