If, Else, Else If, and nested If

If, Else, Else If, and nested If

When learning programming for the very first time, there are certain concepts that can be hard to wrap your head around. One of those is conditional expressions. A simple If statement isn't too bad. If this thing is true, do that thing. Easy-peasy. But then we add an Else, and there is a little more head scratching. It kind of makes sense, but not quite. And then we hit Else If and nested Ifs, and the parallels to "normal thinking" seem far too tenuous.

I wanted a more visual way to describe this than logic diagrams, which honestly never spoke to me. Instead, I thought about caves. Caves with treasure in them. The only rule is, you can never go backward. In the simplest case, there is just one cave. If we pass the test, whatever it is, we get to go in and collect the blue stars.

A single cave entrance with blue stars inside, representing an If statement.

That was fairly simple, so let's look at two caves side-by-side. You can only go in one or the other. If you had a choice, this might be the Lady or the Tiger, but instead you have a test. If you pass it, you go into cave A, Else you go into cave B. In cave A, as before, you get to collect the blue stars, while in cave B, there are fuzzy dots I call tribbles.

An If/Else statement showing a test dictating whether you go to cave A or cave B.

This is still fairly clear, as you get either blue stars or tribbles. You can't get both, and the decision about whether you get one or the other comes from the test.

Now, here is where it gets a little trickier. There are two more caves, or tunnels perhaps, that are inside cave B. It is another straightforward If/Else, but it is nested inside the first Else. Look at the picture and see if you can see it. If the first test passes, you go into cave A and collect blue stars, but you will never get the tribbles and you will never even get to the test for caves C and D.


If/Else with another If/Else nested inside the Else.

So, let's talk this through. There is a test before I go anywhere. If it is true, I go into cave A and collect blue stars, Else I go into cave B. But that test mattered a lot, since I can only get to cave C or D if I went into cave B. Once I get into cave B, I can collect the tribbles. Then I get another test, which determines If I go into cave C and collect green peppers, Else I go into cave D and collect yellow suns.

Think about that carefully and look at the drawing. When you have it in your head, look at the image below, which shows the Else If that confuses people so much. It looks very much like the previous image, because an Else If is very much like an If nested inside an Else. What is the the one difference?

If/Else If/Else statement described in caves.

Fundamentally, the Else If is just a slightly neater way of doing a nested If, with one small difference. You are still doing the first test to determine whether you go into cave A to collect blue stars or into cave B. You still can't get to caves C or D if you want into A. If you went into B, you are still faced with a second test which determine If you go into cave C and collect green peppers, Else into cave D and collect yellow suns. So, the one difference is that you can't have any tribbles to collect, as you have eliminated the space between the Else and the nested If. If you want anything to happen in cave B before going into C or D, you need a nested If.

Finally, let's look at the case where you can go into cave A or B, but you come out the other end before you get to caves C and D.

No alt text provided for this image

Here, you face the first test. If it is true, you go into cave A, collect your blue stars and then exit, Else you go into cave B, collect your tribbles, and then exit. Now, whether you collected stars or tribbles, you get the second test. If it is true, you go into cave C, collect your green peppers and then exit, Else you go into cave D, collect your yellow suns, and then exit.

So, that is conditional logic in a nutshell, or rather in a series of caves.







To view or add a comment, sign in

More articles by Ben Langhinrichs

  • All the web's a stage! (Full Stack version)

    If you've ever been at a social event and heard someone use the term full stack web development, you may have veered…

  • Loops, Camels, and Coconuts

    One of the most important early concepts to learn in programming is the idea of loops. While a statement does one thing…

  • Exception Handling (Babysitter edition)

    Time for another fun article on programming concepts, this time on exception handling. Or, as I like to say, "When you…

  • Arrays and other Collections (at the water park)

    Time for more fun with early programming concepts. This is an update of an earlier article, now with more Collections!…

    3 Comments
  • A Party Inside the RESTful Academy

    Behind almost every web page there are services, and these web services are more often than not built with REST APIs…

  • Inheritance & Polymorphism (& smelly butts)

    I'm trying with these articles to provide simplified, visual representations of basic programming concepts. Inheritance…

  • Arrays, Lists, Queues, and Stacks (at the water park)

    Time for a little more fun with early programming concepts. Most modern programming languages such as Java and C# have…

  • CoexLinks Fidelity: Give 1998 its email back

    If you would be interested in a short, 15 minute online demo, I'd be happy to show you how IBM Notes/Domino email…

Others also viewed

Explore content categories