Lesson 7
Else If, If
Author: Brandon Fajardo (9/16/17). Updated (8/10/19)
Introduction
Review: In the last lesson, you learned about how to use nested if statements and logical operators such as and, or, and not.
Lesson topic: In this lesson, you will learn how to use the else if statement and else statement.
Topic 1 - Else if and Else
In the last few lessons, we learned multiple ways to modify an if statement. We have yet to learn about the else statement, a very useful addition to the if statement. First, let’s set up an example.
EX: Consider three states of matter for water: solid, liquid, and gas. In the Celsius scale, 0 and 100 degrees represent the temperatures that separate these three states. I want to write some code that changes water’s state of matter according to its temperature. As of now, the following code is how you might expect it to be written:
As you can see, we wrote three if statements. But, we can use else statements to improve this code.
We can now simplify the first example of water temperature utilizing else if and else.
As you can see, this is condensed. It no longer requires logical operators as before. Furthermore, there is a simpler logical flow.
To explain how these are used, let’s begin by looking at the notation and logic of these statements.
Subtopic 1 - Else if and Else Notation
Before we begin to learn what is happening, let’s first learn how to write else if and else statements. As you can see in the example above, else if and else statements are used in a chain.
First, there is an if statement.
Then, there is an else if statement.
Finally, the else statement is added to the end.
Here is the basic notation:
Now that you know how to write them, let’s move on to how they work.
Subtopic 2 - Else If and Else Logic
The logic of the else if statement and the else statement is summarized in the following table:
The way that the else if statement and else statement work can be visualized with a flowchart, with one condition leading to the next.
EX: Describing the Logic
Let’s use our water temperature example to detail this logic.
First, the code checks if the temperature is greater than or equal to 100.
If it is, it equates the variable to “Gas” and skips the remaining statements below it.
If it is not, it moves to the next statement.
Then, it checks to see if the temperature is greater than 0. If it is, it equates the variable to “Liquid” and skips the remaining statements below it. If it is not, it moves to the next statement.
Finally, if no previous conditions were satisfied, the variable is equated to “Solid”.
Subtopic 3 - Number of Statements
You are able to vary the amount of else if statements and else statements that you use.
You may use more than one else if statement.
You may use no else if statements and just an else statement.
You may choose not to use an else statement and just else if statements.
Things to Note: You cannot write more than one else statement. Logically, this makes sense because there can only be one default value.
EX: Number of Statements
In this example, we use three else if statements and no else statement.
In this example, we only use an else statement and no else if statements.
EX: User Age
To show what we can do with else if and else, I created a larger chain of statements.
I wish to play a game where I test the user’s age. Depending on their age, different results appear. Notice that I even account for ages that one would not expect such as 0 and negative values.
Problem #1: Grades Try making a chain of else if and else statements that calculates your letter grade on a test based on the percentage you got on that test.
Want a challenge? Also code + and - grades. (A+, A-, etc.)
*Solutions at the bottom of this document
Hint: For example, traditionally any percentage over 90% (including 90%) would be an A.(Assuming you aren’t using + or -)
Topic 2 - Else if and Else vs. Multiple If Statements
Although the else if and else statements may seem better than using regular if statements, there are still many cases in which it is better to use multiple if statements instead. This is because we may want one variable to satisfy multiple if statements conditions at the same time. Else if and else only allow one condition to be satisfied at a time.
EX: Multiple If Statements
To demonstrate an instance in which using multiple if statements may be useful, let’s also learn a new function. Let’s use the “contains” function. Consider a String, x. This simply tests if a certain character is contained within the value of String variable. If this string contains that value, a value of true is returned. Otherwise, it returns false.
Notation:
EX: Here, I test if any of the following digits are contained within the “animals” variable: 3, 1, 7.
In this case, it was more useful to have multiple if statements. That way, all the if statements could be tested at once.
Conclusion
Recap: In this lesson, you have learned the logic behind else if, and else. You also learned about the .contains() function and how else if and else does not always replace multiple if statements.
Next: In the next lesson, you will learn about switch statements.
Solutions
Problem #1:
Here, I show the way I have coded this problem. You will get some variation within your answers. Make sure to test the edge cases such as 90, 80, and so on to make sure they work properly.
Last updated