Skip to main content

Section 14.4 MATALB if-elseif-else-end Statements

Previously, we just saw how we can use an if-else-end statement to essentially create two blocks of code that can be run independently depending on the outcome of the conditional statement. There is another MATLAB conditional structure that is even more flexible and actually allows you to have however many independent code blocks you would like. The if-elseif-else-end statement allows this.
Generically, the if-elseif-else-end statement looks like this:
if < conditional expression #1 is true >
< code block 1 >
elseif < conditional expression #2 is true >
< code block 2 >
else *technically the else here is optional
< code block 3>
As you can see, this is very similar to the statements we have been covering thus far. However, there are a couple of things to note:
  • Technically, the else is optional in an if-elseif-else-end statement. If you look at the flowchart in figure 14.4.1 below, omitting the final else would be equivalent to chopping off the < code block 3 > chunk of code.
  • In this generic example there is only one elseif statement with a corresponding conditional expression. However, MATLAB lets you have however many elseif statements that you need.
  • Note that because of the logic of the statement, you only need 2 conditions to run 3 separate blocks of code. In general, when designing an if-elseif-else-end statement that has ( n ) different conditions, you need ( nβˆ’1 ) conditional statements.
The generic if-elseif-else-end flowchart is shown below in figure 14.4.1. Again, keep in mind it can have any number of elseif < conditional expression > lines!
Figure 14.4.1. The generic flowchart for the if-elseif-else-end statement.
Student Grades - if-elseif-else-end Example
I think that it is important to clarify the statement β€œwhen you have ( n ) different conditions, you need ( nβˆ’1 ) conditional statements”. To do so, let’s consider an example. Let’s say that we have a variable grade that contains the final numeric grade for a person in a class. Furthermore, let’s say that this hypothetical class does not have + or - grades, instead, the final letter grade is calculated as follows:
Figure 14.4.2. Student grading scheme example.
The problem is to create a MATLAB script that can take the numeric grade as a variable grade, determine the letter grade associated with it, and store that in a variable called letter_grade.
This may seem contrived because, in this particular example, we will only have 1 grade but in the next chapter, we will learn techniques that will allow us to analyze any number of grades in the same fashion. What we are working on now will be the bones of that future algorithm!
Stop and Think
Think and Sketch - we can see that we have 4 different categories of grades. That means that ( n=4 ) (the grade can be an A, B, C, or F). Before you continue, can you create the flowchart necessary to complete this script? Take your time. I am hoping that by thinking and sketching it will be obvious why you only need ( n–1 = 3 ) conditional statements.
If you are feeling like a tough workout today, go ahead and try to program this script on your own. Go you! If your brain is too tired from that it is OK, but don’t skip the flowchart part.

Checkpoint 14.4.3. Thinking About Code Flow.

Think about the possible flow of your code. Write down initial thoughts, questions, or ideas.
If you are struggling to start, think about what you are trying to accomplish. How many different if/else statements will you need?
I won’t threaten you with puppies or lemurs anymore. I will trust that you care about your education and want to learn this material. Handshake?
Figure 14.4.4. Our handshake agreement that you are working on the material. I love stock images so much.
Here is my flowchart for the problem.
Figure 14.4.5. My flowchart for the letter grade problem.
Take a moment to compare our flowcharts.
  • Did you start from high to low? That is a common way of doing things and totally fine. Maybe I am just weird? It doesn’t matter.
  • Do you notice how my flow chart only has three conditional statements? THAT is what I meant by ( n ) conditions requires ( n–1 ) conditional statements. Here is the logic:
    if you scored less than 70, you get an F
    elseif you scored less than 80, you get a C
    elseif you scored less than 90, you get a B
    else, you get an A. (You do not need a conditional statement, because if you didn’t get an F, C, or B, the only option left is an A!)
Try It!
Now is your chance to try and do the code, test, and repeat part of our process on your own to finish the problem! Even if you can’t get it working perfectly, take some time and give it your best shot! Make sure you have a pen and paper handy and jot down notes, ideas, and snippets of code as you are working. You got this!
My script solution follows in figure 14.4.6

Example 14.4.6. Chapter 14 Grading Script Solution.

Here is an interactive code box that functions identically to the MATLAB command window.
You have attempted of activities on this page.