The Conditional Statement in MATLAB

Ammar Ali Feb 02, 2024
The Conditional Statement in MATLAB

This tutorial will discuss defining the conditions for code execution using the conditional statements in MATLAB.

Conditional Statements in MATLAB

Like other programming languages, MATLAB also provides many conditional statements like the if, if-else, if...else...if, nested if...else, and switch statement.

First of all, let’s talk about the if statement. We can use the if statement if we only want the code to run at a specific expression.

The code inside the if statement will only be executed if the condition is true. To define a condition, we can use arithmetic and logical operators.

The arithmetic operators include the less than, greater than, equal to, less than, and equal to, greater than, and equal to. The logical operator includes the and operator and or operator.

The logical operators are utilized to combine two or more conditions. We can use the and operator if we want the code to execute only if two or more conditions are true.

We can use the or operator if we want the code to execute only if one or more conditions are true. For example, consider we want to display the text GreaterThan only if the value is greater than 0.

See the code below.

value1 = 5;
if(value1 > 0)
    disp('GreaterThan')
end

Output:

GreaterThan

In the output, the text is displayed because the value is greater than 0.

Now consider, we want to display LessThan text if the value is not greater than 0. In this case, we can use the if...else statement.

See the code below.

value1 = -5;
if(value1 > 0)
    disp('GreaterThan')
else
    disp('LessThan')
end

Output:

LessThan

The LessThan text is displayed in the output because the value is not greater than 0.

In the case of the if...else statement, if the condition is true, then the code inside the if statement will be executed. If it’s false, the code inside the else statement will be executed.

Now, consider, after checking if the value is greater than or less than 0, we also want to check if the value is equal to 5 or not, and if the value is equal to 5, we will display ValueIsFive text. In this case, we can use the nested if...else statement.

See the code below.

value1 = 5;
if(value1 > 0)
    disp('GreaterThan')
    if(value1 == 5)
        disp('ValueIsFive')
    end
else
    disp('LessThan')
end

Output:

GreaterThan
ValueIsFive

In the output, two texts are displayed because the input value is greater than zero, and also it’s equal to 5. The if...else...if statement can be used if we only want to check if the first statement is not true.

For example, consider, we want to check a value if it’s less than 0 and if it’s equal to -5 or not. See the code below.

value1 = -5;
if(value1 > 0)
    disp('GreaterThan')
else if(value1 == -5)
        disp('Value = -5')
    else
        disp('LessThan')
    end
end

Output:

Value = -5

We can also use a switch statement instead of a ladder of the if...else...if statement. For example, let’s convert the above code to a switch statement.

See the code below.

value1 = -5;
switch(value1)
    case 0
        disp('value = 0')
    case 5
        disp('value = 5')
    case -5
        disp('value = -5')
    otherwise
        disp('Default Value')
end

Output:

value = -5

Now, let’s use the logical operators to define multiple conditions in an if statement. See the code below.

value1 = 5;
if(value1 > 0 && value1 < 100)
    disp('Value is between 0-100')
end

Output:

Value is between 0-100

In the code, the text will only be printed if the value is between 0 to 100.

Suppose we use the && operator, the code inside the if statement will only be executed if all the conditions are true. Suppose we use the || operator, the code inside the if statement is executed if one or more conditions are true.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook