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 trialMichelle Kang
2,763 PointsIs an else if structure superior to an if(and) structure?
Wondering if there is an advantage to structuring the code with fallbacks and new conditionals on the else clause, as opposed to starting with an "if __ and if ___ and if ___ etc" clause and leaving else only if... all the ifs fail.
2 Answers
Devin Scheu
66,191 PointsIf none of those if statements are true for an unknown reason than the code challenge would break unless you have an else. That is why else is so important. For example if you check for an integer and a string, but the value you got was a null, if you didn't have it if statement for it the code would break, else covers all other possibilities that's why it is so effective.
Juan de Elena O'Shea
4,757 PointsHello!
I think that by your question, you mean if __ else if __ else if ___ and not the and ifs you state (which would add tests to the variable before it is declared true or false).
Regarding the fallbacks with the else statements, I guess they are useful, not as to avoid the program breaking (since if the condition is false, then the program does nothing about it and continues), but rather to explicit an outcome when needed (ask for a proper input, etc).
Hope it helps!
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsJust to go into further detail, adapt the code to what you need. Think about the logic of what you're trying to do in plain English (or your preferred language) and then convert it into actual code. This is called pseudocode, if you've never heard of that i.e.