In Matlab, there are several statements that need to be executed as per the conditional statements. If one statement is true, it will provide the result; otherwise, it will jump to another statement, then produce the result accordingly. All these statements are based upon the decision-making method in Matlab. This blog will provide you information on if else statement in Matlab and what is the syntax of this statement. But, before proceeding to the details, let’s get brief details on decision making in Matlab.
What is decision making in Matlab?
Its structure needs that the coding must have one or more conditional statements that are tested or executed by a programmer. If the given condition is true, then it will execute; if not, the next statement will be executed of the program.
The flow of this can describe how the conditional statements work:
There are different types of conditional statements that are used for decision making, such as if statement, if else statement, nested statements, and much more. All of these are used for different purposes. Now, let’s get details on what is if else statement in Matlab.
What is if else statement in Matlab?
- For the false expression, an if statement is always followed by an else statement.
- There are three statement parts in Matlab that are if statement, if else statement, else if statement.
Syntax
if <expression>
% statement(s) will execute if the given boolean expression is true
<statement(s)>
else
<statement(s)>
% statement(s) will execute if the given boolean expression is false
end
If the given boolean expression executes to be true, then the block of the if statement would be executed; if it is not so, then the block of else statement code would be executed.
Flow diagram
The working of the if else statement in Matlab can be easily understood by the flow diagram that describes that first, the programming will be checked by the software, and then the decision-making process will be done. If the statement is found to be true, then it will go to if code condition, and if the statement is false, then it will jump to the else code block after that the result will be produced by the Matlab software. Now, let’s check some of the programmings of this conditional statement.
Example of if else statement in Matlab
Example 1:
Input:
b = 50;
% now it will check the given boolean condition
if b < 10
% if the given condition is true, then it will print the following output
fprintf(‘b is less than 10\n’ );
else
% if the given condition is false, then it will print the following output
fprintf(‘b is not less than 10\n’ );
End
fprintf(‘value of b is : %d\n’, b);
Output:
When the above-mentioned program is executed, then it will produce the result:
b is not less than 10
value of b is: 100
Example 2:
If one wants to write a program to compare the two numbers x and y, then they can execute this program.
Input:
x = 5 and y = 10
clc ;
x = 5
y = 10
if ( x > y ) – – – – – consider as condition 1
disp ( ‘ x is maximum ‘ ) – – – – – if the condition 1 is true
else
disp (‘ y is minimum ‘ ) – – – – – -if the condition 1 is false
End
Output:
x= 5
y= 10
y is maximum
Example 3:
If one wants to execute the nested statement that has a number of conditions, then if else statement in Matlab can be used. Let suppose you have three different numbers x, y, and z with the values of each x = 5, y = 10, z = 15, then the program will be written as:
Input:
clc ;
x = 5
y = 10
z = 15
if ( x > y ) – – – -this is condition 1
if ( x > z ) – – – this is the nested if condition 2
disp ( ‘ x is maximum ‘ ) . . . .this will execute if condition 2 is true
else
disp ( ‘ z is maximum ‘ ) – – – -this will be executed if condition 2 is false
end
end
if ( x < y ) – – – – – -this is condition 3
if ( y > z ) – – – – this is another nested if condition 4
disp ( ‘ y is maximum ‘ ) if the given condition 4 is true
else
disp(‘z is maximum’) this will execute if condition 4 is false
end
end
Output:
x = 5
y = 10
z = 15
z is maximum
Example 4:
If one wants to use the logical operators, then they can use any of these programmings:
1 if x = 5
Input:
Clc ;
x = 5
min = 1
max = 10
if ( x > = min ) & & ( x < = max )
disp ( ‘ x is within range ‘ )
elseif ( x < = min )
disp ( ‘ x is less than minimum ‘ )
else
disp ( ‘ x is more than maximum value ‘ )
end
Output:
x = 5
min = 1
max = 10
x is within range
2. If x = 30
Input:
clc ;
x = 30
min = 1
max = 15
if ( x >= min ) & & ( x < = max )
disp ( ‘ x is within range ‘ )
elseif ( x < = min )
disp ( ‘ x is less than minimum ‘ )
else
disp ( ‘ x has maximum value ‘ )
end
Output:
X = 30
min = 1
max = 15
x has maximum value
3. The value of x = 2
Input:
clc ;
x = 2
min = 1
max = 10
if ( x > = min ) & & ( x < = max )
disp ( ‘x is within range ‘ )
elseif ( x < = min )
disp ( ‘ x is less than minimum ‘ )
else
disp ( ‘ x has maximum value ‘ )
end
Output:
x = 2
min = 1
max =10
X is less than minimum
Conclusion
This blog has provided all the relevant information about if else statement in Matlab with its syntax and examples. With the help of if else statement, one can easily solve complicated conditional issues. Besides this, one can use nested if operations to provide condition within the single condition. In this blog, expression involves several logical operation, such as ‘<’ (less than), ‘>’ (greater than), ‘> =’ (greater than equal to), ‘= =’ (equal to), ‘< =’ (less than equal to), ‘=!’ (not equal to), ‘&&’ (logical and), ‘||’ (logical or), and much more.If you have any issue regarding any Matlab assignments, then you can contact our experts who offer Matlab Programming Assignment Help to the students living all around the globe. We offer assignment with proper syntax and well-structured programming which is easy to implement as well as easy to understand. Our services are accessible 24/7 and offer at minimal prices.