# Conditional Statements in Matlab Programming, if, if-else, and else if

Table of Contents

**Conditional statements in MATLAB:**

**Conditional Statements in Matlab-** In this tutorial, we will introduce you to the conditional statements if, if-else, and else if; and then I will show you through examples, how to implement each of these statements in **MATLAB**. Let’s get started.

Before we jump into **MATLAB Programming**, let’s take a look at the **structure** of the **if statement**. In a **flowchart of the if statement** as given below, **flowcharts** are super useful for documenting the paths that a **program** can take. So, we start the **program** with some sort of input and we evaluate this input using a **logical expression.** This logical expression is going to consist of one or more of these **relational operators**.

## if Statement in Matlab:

Such as **less than**, **greater than or equal** **to** and if this **logical expression** evaluates is true then the program will execute one or more statements and then end. However if this **logical expression** is false the program will just end. So here is a **flowchart** of the example that we are going to work in **MATLAB** and in this case what we are trying to do is take an input grade.

In this example 65 and if this grade is greater than or equal to 60, if that is true we will know we have passed the class and the program will end.

So let’s go over to **MATLAB** now and implement this. So we are start off our program by entering a numeric grade and we are going to store it in a variable called grade because that just makes sense. So in the grade variable we are going to assign it a value and you can assign it anything that you would like. In this case I will choose let’s say 70 then I will add a semicolon on the end of this line to suppress the output and next we will move to implementing our **if statement,** so we say if our grade is **greater than or equal** to 60 this is our logical expression and the **greater than or equal to** is our **relational operator.**

1 2 3 |
Grade = 70; If grade >= 60 |

So if the grade is greater than or equal to 60 then we want the program to display that we have passed the class, so we are going to use the display function to do so. So in this case, we will display pass and then we will end the program.

1 |
disp('pass') |

So this is how this would read if the **grade is greater than or equal to 60** then display **pass** and in the program if that logical expression evaluates as false the program will just end.

1 2 3 4 5 6 7 |
grade=70; if grade>=60 disp('pass') end |

So let’s run the program and see what we get here output to the command window. So I click run and we get **pass** and that is what I would expect because my grade was **greater than or equal to 60**.

Output:

Now let’s go back up and change this input grade to something less than 60. Let’s say 55 and now we have run the program and you can see here that nothing happens and that’s because there’s only one path that the program can take to actually evaluate a statement.

So since the grade was not greater than or equal to 60 the program just ended.

Now hopefully you can see here that one of the major limitations of the **if statement** by itself is that we only have one path that the program can take it would probably be preferable to have the program tell us that we have failed the class rather than just ending and not really displaying anything for us.

**Matlab Else statement:**

So this leads us to the **else statement** so here is the **structure** and a **flowchart of the else statement**

So again we start our program with an input and evaluate that input with a **logical expression** and if true statement group 1 is evaluated and the program ends. Now unlike the **if statement** if this logical expression is false a second statement group can be evaluated before the program ends. So now let’s look at our **flowchart** that we’re going to implement in **MATLAB**.

Now in this case we start off with a grade again and we are trying to determine if we have passed or failed a class, so we evaluate this input based on our **logical expression** and in this instance our grade of 55 is not greater than or equal to 60. So since that is false our second statement group is evaluated in the program ends.

So let’s return to **MATLAB** and revise our program to implement the **else statement**. So the purpose of using the **else statement** is to add a second path that the program can take and this ends up being really easy to implement, so right now we have if the grade is greater than or equal to 60 then we want to display pass or else if that **logical expression** is not true then we want to display fail and that’s actually it.

1 2 3 4 5 6 7 8 9 10 11 |
grad=55; if grad>=60 disp('pass') else disp('fail') end |

So now the program has two different paths to take based on this **logical expression** and if it evaluates as **true** or **false**.

So right now we have a grade of 55 input into our program, so let’s run the program and we see that it does say fail and so this program is working correctly.

**Matlab Else if statement:**

Now while this is much more desirable than just using an **if statement** alone what if we want to have more than one **logical expression**. So for example it may be more desirable to show the letter grade that we have received in the course. However we would need to use more than one **logical expression** to find out what letter grade that we have received and this leads us to the **else if** **conditional statement**. So here is the **structure** and a **flowchart** of the **else if statement;** the benefit of using an **else–if statement** is it allows us to use more than one **logical expression**.

So we start off our program again with an input and evaluate it with **logical expression** one and if true then we evaluate statement Group one and in the program **else if logical expression** one is false then we evaluate a **logical expression** two and if true then we evaluate statement Group two and in the program **else logical expression** two is false then we evaluate statement Group three and end the program. So now we can see a **flowchart** of implementing the **else if statement** in the program that we have been working on, in this case we are going to use **multiple logical expressions** to determine the letter grade that we have received in the course instead of just saying that we have passed or failed. So now we have multiple paths that our program can follow so again we start with a letter a grade as our input.

Now for example, we have number in subject is 72 and we evaluate it with our **logical expression** and in this case it is not true the grade is not greater than or equal to 90. So that is false and we move on to our second **logical expression** now in this case the second **logical expression** is also false and we move on to our third **logical expression**. Now finally, our input grade of 72 in this example is greater than or equal to 70 that is true so we have received a letter grade of C and our program ends.

So let’s return to **MATLAB** and implement the **else if** structure so we are have to modify our program quite a bit to implement the **else if conditional statement**. So let’s go ahead and start from the beginning so I am going to erase our **if** and **else statements** that we used previously and now we are have multiple different **logical expressions** not just one and that’s because we have several different letter grades that we want to be able to assign based on our input numeric grade. In the **else if statement** we will take the input from the user he will enter the subject marks by using the command.

prompt = ‘Enter the subject marks = ‘;

grad = input(prompt)

**Code:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
prompt = 'Enter the subject marks = '; grad = input(prompt); if grad>=90 disp('A') elseif grad>=80 disp('B') elseif grad>=70 disp('C') elseif grad>=60 disp('D') else disp('F') end |

**Code Explanation:**

Now we are gonna start out with our first **if statement** and our first **logical expression**. So if our grade is greater than or equal to 90 then we have received in “A” in the class

if grad>=90

disp(‘A’)

else if our grade is greater than or equal to 80 this is our second **logical expression** then display that we have received a grade “B” in the class

**elseif grad>=80**

**disp(‘B’)**

else if our grade is greater than or equal to 70 then display that we have received grade “C” in the class

**elseif grad>=70**

**disp(‘C’)**

else if our grade is greater than or equal to 60 then display we have received grade “D” in the class

**elseif grad>=60**

**disp(‘D’)**

And that’s going to be our last **logical expression** because if none of the previous **logical expressions** are true then our only other option is that we have received grade “F” in the class.

**else**

**disp(‘F’)**

So I am just going to use an **else statement** here because we only have one other option and we will display an Fand then we will end our program. So right now we will click run

Now we will enter the marks in the command window in order to find the grade of the subject. You see that we have an output and F to the command window and this is because the numeric grade that we entered at the beginning of the program was a 55 and so that is less than or equal to sixty. our last **logical expression** and so yes that did result in an F in the course.

Now we will give subject marks equal to 80 so the output will be: