If, Elseif and Else statements are important in such programming languages along with MatLab. These logic statements are used in lots of codes to obtain queries for different conditions for different variables. In here, we explain the use of if, else and elseif queries in Matlab with very basic examples below.

Table of Contents ;

## How To Use ‘if’, ‘else’ and ‘elseif’ Codes In MatLab?

When you use these logical queries in Matlab, you need to generally use logical operators** **in Matlab. With these logical operators, you can create your queries for different conditions.

As you know the meaning of ‘if’ in English Language, ‘if’ is used to obtain basic logical conditions. Take a look at the example below please.

```
>> x = 4; multi = 70; div = 32;
if x<5
%external 'if'
multi = 4*x
div = div/x
if x<3
%internal 'if'
multi = multi*4
end
end
multi =
16
div =
8
```

At above example, we created three of variables; ‘x’, ‘multi’ and ‘div’. We gave them starting values to them, 4, 70 and 32 respectively. These values can be taken from the user, or can be created randomly in Matlab.

We used two of ‘if’ expressions one of them inside antoher one. The external ‘if’ has the condition of ‘x>5’, which means if the value of ‘x’ is bigger than 5, the statement or code inside this if will work.

We added another ‘if’ for multiplication, inside the external ‘if’, the condition for this if is, ‘if x smaller than 3’. For example, if the value of ‘x’ equals to 4, the first multiplication and divison operation will be done, but the internal ‘if’ operation will not.

According to the program, check the new values of ‘multi’ and ‘div’ at command window above in Matlab.

Every ‘if’ is closed with ‘end’ as you see above example.

## The Use Of ‘else’ In ‘if’ Codes

As we stated above, ‘if’ code gives a condition to work the operation inside it. If this condition is valid or true in programming logic, the statement or code inside the ‘if’ will work.

Also you can add ‘else’ between the ‘if’ and ‘end’ codes in Matlab. You can add codes beneath the ‘else’ code, for the situation where teh condition for ‘if’ is not valid or false in programming logic. Take a look at the very basic example below.

```
>> x = 5;
%starting value of x
if x <4
%condition about the value for x
x = 6
%if the condition is true, the new value of x
else
x=3
%if the condition is false, new value of x
end
x =
3 %the result
```

As you see in above code in Matlab command window, ‘x’ has a starting value. This value can be taken from the user or result of an another code. As you see above there are two conditions for ‘x’. ‘x’ will take according to these conditions.

**YOU CAN LEARN ANSYS IN MECHANICAL BASE; Click And Start To Learn ANSYS!**

So in here, ‘x’ took the value of 3, which is beneath the ‘else’. Because ‘x’ is not smaller than 4, which is the condition of ‘if’ above.

The logic of ‘else’ is very simple like above.

## The Use OF ‘elseif’ In MatLab

But, you can use ‘else’ for one time in the ‘if’ code. If you want to add multiple of conditions inside ‘if’ code, you need to use ‘elseif’. Check the example below to understand.

```
>> x = 3;
if x<1
x=5
elseif x==1
x=6
elseif x>1
x=8
end
x =
8
```

Probably, you understand the logic of ‘elseif’ command above. Unlike ‘if’ and ‘else’, you can add numerous of conditions like above example in Matlab.

So the use of ‘if’, ‘else’ and ‘elseif’ in Matlab is veyr simple like this. The topic of this article, to give the understanding about the use of ‘if’, ‘else’ and ‘elseif’ in Matlab. Also this article can be a reference about the syntax of ‘if’ ‘else’ and ‘elseif’ in Matlab.

If you wish more complex examples about ‘if’, ‘else’ and ‘elseif’ codes in Matlab, please express this in comments below.

Do not forget to leave your comments and questions about ‘if’, ‘else’ and ‘elseif’ codes in Matlab below.

Your precious feedbacks are very important for us.

## Leave a Reply