In this blog we are going to be discussing an extension to the if statement called the else if statement. Now, the else if is different than the if else…Which is confusing because the names are so similar. Soon I will go through all of the differences so you can have a clear definition of all three.
Else If is something you will want to use when you want to check for more than one thing with an if statement.
For example, let’s say we have a variable, called temp.
With an if and an else, we can check for two states…
This is useful, but with an if-else we are able to check for three or more states.
The key here is that it works just like a second if statement. In fact, there is not any rules on what you have to test inside of the second condition. You are allowed to use other variables or whatever you want. Generally, though, you will be checking for the same thing in a different range.
So, the easiest way to think about this is that the else at the end is appended to either the if statement or an if statement with the additional else if. This is a “catch all” section that will always run if none of the other conditionals get ran.
Now, it’s important to realize that only one section will ever get ran. It’s not possible for both the if and the else-if to run!
Are you ready to read the next blog? Let’s go!