In today’s lesson, we’re going to cover the following topics:
1. If-Else Statements
2. String Comparison
video coming soon
If-Else Statements
Using an if-else statement is useful if you want to execute specific code if a condition is met. This is one of the 4 types of control flow logic (recall the 4 types – if-else, switchcase, while loops and for loops).
Here is the basic syntax:
Let’s go through each line. The words in blue are mandatory and the words in black will be your own code. However, if you don’t have too many scenarios to execute, only the if and end are really necessary – the elseif and else are not.
(a) if
If is the first line, and this is followed by an expression. This means that if that expression you put there is true, then MATLAB will execute the statements in the second line.
(b) elseif
The elseif line is not mandatory, but is there if you have other scenarios you want to identify. In this case, you will put the statement you want MATLAB to check as the expression next to elseif.
(c) else
The else line is there to cover all other scenarios not covered by if or elseif. This does not have any expression next to the word else.
(d) statements
Everywhere it says statements in the image above, you will type the code of what you want MATLAB to execute if the condition for that block is true. For example, if the expression in the if line is true, then MATLAB will execute the statements right under it, and so on for the other sections.
(e) end
You always need to put the word end at the end of the if-else block. If you don’t have the end statement, MATLAB will continue to execute all the code under the else line because it was the last part of the if-else block, even if you don’t mean to include it or execute it as part of the if-else block.
Let’s do an example. Say we want the user to enter a number, and then we want to check whether that number is positive, negative, or 0. For each of these 3 scenarios, display “The number is positive,” “The number is negative,” or “The number is zero.”
Here is a way to think through the problem:
1) The question says that the user should enter a number. This means that we need to create an input statement to get a number form the user. I will create an input statement and set it equal to the variable x.
2) Now we want to check whether the number that the user entered is greater than, smaller than, or equal to 0. Here we will use Relational Operators to compare the value of the input to being greater than 0 (x>0), smaller than 0 (x<0), or zero. Click here if you missed the lesson on Relational Operators.
3) Within the statements sections, we want to display either “The number is greater than 0,” “The number is smaller than 0,” or “The number is zero,” as the question asked.
Now let’s put all of these together into a code. Because we have 3 scenarios – either x is greater than 0, smaller than 0, or equal to 0 – so I used one scenario with the if section, one with elseif, and the last scenario was with else. I didn’t have to type a code that x = 0 like I did with x>0 and x<0, because x = 0 is the only other scenario so it will be covered if I just leave within the else block.
It doesn’t specifically have to be the x = 0 scenario – it could’ve been any of the three scenarios that you put under the else block.
Now let’s run this code and see what comes up in the Command Window:
First, the input statement prompts the user to enter a number:
I entered 10, and this is what came up. It says that the number is greater than 0, which is true.
Then I tried running the code again and entering -4. It displays that the number is smaller than 0, which is true:
Then I tried entering 0, and it displays that the value is 0:
String Comparison (using Strcmp)
What if we want to compare strings (words instead of numbers) using if-else? Here we will use a function called strcmp. We will set up the same if-else construct as the first example, but will replace the expressions with strcmp and the strings you want to compare. This will replace the sections I had put x<0 or x>0 in the example above.
Let’s do an example where I want the user to enter the name of a fruit. If that fruit is either an apple, pear or banana, then it should display a sentence stating the fruit’s color (eg. if the user enters “banana,” MATLAB will display “this fruit is yellow,” and so on). If the fruit entered is not in this list, MATLAB will display “the fruit is not in the list.”
Here is a thought process to get the answer:
1) This is the same as the first example. We will need an input statement for the user to enter the name of a fruit, and we can call this variable fruit. Because a string is now being entered (text rather than numbers because the name of a fruit contains letters), we will need to use ‘s’ in the brackets of the input statement to indicate to MATLAB that it needs to accept a string.
2) We will need to use strcmp to compare the name of the fruit entered with the fruits apple, pear, or banana as the expressions. This will work by typing strcmp(variable name, what you want to set the variable equal to). For example, the name of the input variable is fruit, and say we want to check whether that fruit is a banana. So we type strcmp(fruit,’banana’). You usually need to have single quotes around each word so that it turns purple like in the code below. In this case I have used single quotes only around the word banana because the input statement already treats the variable fruit like a string, since we previously had to use ‘s’ in the input statement.
3) We will use disp statements to display what the question asked – the color of the fruit entered, or “this fruit in not in the list.”
Putting this together in a code gives the following:
Running the codes gives this in the Command Window:
Entering “banana” as an input then executes the rest of the code, and displays that the fruit is yellow:
Running the code again and entering a fruit not on the list, such as “mango”, outputs that the fruit is not on the list: