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

iOS Objective-C Basics (Retired) Functional Programming in C Conditionals - If-Else

Adam Warner
Adam Warner
546 Points

else-if statements not discussed but used

I'm a little confused as to why, in the second half of the "Conditionals - If-Else" video, "if" then 2 "else if" statements are used but not discussed by the instructor at all. He continues on and closes it out with a "else" statement learned about earlier. Are we just to assume that "else if" statements are used if more than one "if" statement exists?

6 Answers

Multiple if...else statements can be nested to create an else if clause:

if (condition1) statement1

else if (condition2) statement2

else if (condition3) statement3 ... else statementN

To see how this works, this is how it would look like if the nesting were properly indented:

if (condition1) statement1


if (condition2) statement2


if (condition3) ...

is this the code you are referring to?

int main() { char a = 'a'; char b = 'b'; char g = 'g';

char letter = 'z';

if (letter == a) printf("letter %c is %c\n",letter,a);
else if (letter == b) printf("letter %c is %c\n",letter,b);
else if (letter == g) printf("letter %c is %c\n",letter,g);
else printf("letter %c not found\n",letter);

return 0;


It checks if char letter is = to char a,b,g. I this case its not so it will print letter z not found. If letter = g the last else if statement will print letter g is g.


Adam Warner
Adam Warner
546 Points

Thank you John. That is what I was referring to. I do understand the reason for the code (and the result it spits out). What I didn't understand is the "else if" statements. Although I get what they are doing, up until this point I have only seen "if" then "else" in a code block. The second half of this video throws out two "else if" statements right after an "if", then follows it up with the more familiar "else" statement.

The coloring you are presenting here (orange else and yellow if) makes it look like an "if" and three separate "else" statements, BUT Xcode colors those "else if" lines as one unified color, making it seem as if it is a whole new command.

That is the source of my confusion. At the very least, it seems like given the way Xcode presents it, it should at the very least be mentioned in the video.

Hope this helps, always have a look at online material or the help section in Xcode.

Adam Warner
Adam Warner
546 Points

A nested "else-if" clause - that's what I was needing! I needed someone to explain to me what the multiple "else if"'s were. That is a huge help. Thank you!!!