Skip to main content

Section 14.2 MATLAB if-end Statements

The most basic form of a conditional statement is the If-End statement. Generically in MATLAB, an If-End statement looks like this:
if < conditional expression is true >
< do this >
Let’s break this down:
  • The MATLAB conditional statement must start with the keyword if
  • The if is followed by a conditional expression. Recall that the < > indicate that you need to replace this with actual MATLAB code. A conditional expression is simply an expression with at least one relational operator and any number of logical operators. For example: x > 12 is a conditional statement. So is angle > 25 & angle < 100.
  • Next, we have < do this stuff > . This is simply the MATLAB code that we want to execute only if the conditional expression evaluates to true. MATLAB will automatically tab the text within the decision structure over to make it easy to read.
  • Finally, the decision statement must be completed with the keyword end.
The generic flowchart for this can be found in figure 14.2.1 below:
Figure 14.2.1. A generic if-end statement flowchart.
Example- Hourly Worker Calculation
To see this in action let’s create a short script that calculates the weekly wage of an hourly worker. This program can be used to calculate the pay based on the number of hours worked and the worker’s hourly wage using two variables: hours_worked and hourly_wage. If the worker works over 40 hours a week, they are compensated at 1.5x their normal hourly rate for only the hours worked over 40. For example, if a worker worked 42 hours for $10 an hour, they would get paid $10 per hour for the 40 hours and $15 per hour for the extra 2 hours over 40.
This is a perfect example of using our mantra: think, sketch, code, test, repeat! Do not skip your brain workout for these sections. For each of the prompts below, I am expecting you to stop and actually work on this particular problem before moving on.
Stop and Think
Think - during the think phase you need to ask yourself and then answer a bunch of questions. I can’t think for you but I can give you an example of my thinking process when solving the problem. Keep in mind that you might have other questions that need answering! That is OK! This is what I came up with as I was thinking.
  • Do you understand what the problem is asking?
  • What math is necessary to complete this problem?
  • Can you calculate expected answers based on a few different variables? That way you will have numbers to compare your program against to make sure it works. Example: if hours_worked = 42 and hourly_wage = 10, we would expect pay = 430.
  • How can you have the program calculate the correct value algorithmically? Remember, that means you need to write down the process into steps. Start with calculating the pay in the situation for under 40 hours a week. Then move on to what would happen at 41 hours a week…
  • What conditional statement, if any, is needed?

Checkpoint 14.2.2. THINK about Hourly Wage.

What information do you think is important for solving this problem?
Are there any details that seem unclear, missing, or confusing?
What is the hourly cutoff for different wages? What is your dependent and independent variable(s)?
Sketch- during the sketch phase, you need to draw a flowchart or write out a pseudocode for your algorithm that will solve this problem. The sketch phase will often require several repeats of the think stage. As you are sketching more questions may pop into your head about how to best solve this problem. Before scrolling down below, take a minute and try to sketch out a flowchart or write out pseudocode for this particular problem. Don’t skip out on giving it a try!

Checkpoint 14.2.3. Ordering Algorithm Steps.

Figure 14.2.4. Every time someone scrolls and looks at my flowchart before creating their own, this puppy doesn’t get a snuggle! Don’t be a monster! I was going to say the puppy gets hit but that is too dark. Look how cute he is!
The flowchart that I created for this particular problem is located below in figure 14.2.4.
Figure 14.2.5. My flowchart for the wage program. Note how there is both descriptive text and rough code.
Take a look at the flowchart. Hopefully, it looks very similar to yours. Ask yourself the following questions before moving on.
Do not move on to the next section until you understand the flowchart above in figure 14.2.4.
Code - Now comes the fun part, translating our sketch into code! I highly encourage you to try this on your own before you look at my solution below! It is OK if you get stuck and struggle! That is what learning is about. Take a moment to review the generic version of the if-end statement (in Chapter 17) so you have a good idea of the MATLAB syntax necessary to complete the problem.
Figure 14.2.6. The lemur of judgment is majestically judging those who scroll down before trying to code on their own. Don’t let the lemur down!
Did you try it on your own first? Good! Does your script work? If yes, good work! If not, it is OK! Struggling and practicing IS learning!

Example 14.2.7. Follow Along with Me.

Here is an interactive code box that functions identically to the MATLAB command window.
Example of my script file that calculates the pay. Notice I used sections (%%) to keep things organized and I have nice headers and comments explaining what each section does.
When you run the script with these example numbers, hours_worked = 42 and hourly_wage=10, you can see that the value of the pay = 430!
Test - the testing phase might be the most fun. Just spend some time with the script to double-check that it works. Try substituting different values for variables and verifying by hand that the calculation works. This is an important step and becomes even more important the more complicated your scripts become! Just play around with it and make sure it works the way that you expect.

Example 14.2.8. Fix the Code.

Here is an interactive code box that functions identically to the MATLAB command window.

Checkpoint 14.2.9. Overtime Pay Calculation.

Hopefully, by this point, you understand how the if-end statements are structured and how they are written in MATLAB. The reality is that there has been very little new material in this chapter. We have simply learned how to apply previous concepts to new problems. There are a few other decision structures MATLAB has that we should investigate.
The rest of the MATLAB conditional statements will be explained below and some example code will be provided but the think, sketch, code, test, repeat process will not be replicated in this much detail. The good thing is that the other MATLAB conditional statements are very similar and follow almost the exact same syntax.
You have attempted of activities on this page.