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 trialTaylor Porter
2,338 PointsIf, Elsif, Else Statements VS. Case Statements
I was on the Ruby Operators and Control Structures videos when Jason Seifer explained using Case Statements over If, elsif, else just to save time. Well then what's the point of the If, Elsif, Else statements if you can just use Case Statements all the time?
2 Answers
Krishan Wanarajan
10,939 PointsCase Statements: Basically used for certain variables that are passed through. E.g Kind could be "Bad", "Good", "Average". The so called condition is open to only certain, specific variables.
If, Elsif, Else: Here you can have more flexible with the condition. For example you can have more than one conditional on an If statement.
This is when specificity comes into play. E.g, Sometimes a variable can produce multiple results and you only want to deal with input values thats are greater. This is when If, Elsif and else statements can be handy. Likewise, if you want to only deal with certain, specific input variables, you can use case statement, because in essence you have only a certain set that you can input, if that makes sense.
Kristian Terziev
28,449 PointsWell the point is that you can compare different things (variables for example) in the if and in the elseif. Wen you use case you use one varible in all comparisons.