MATLAB – Conditional Statements
Conditional statements are something that is very basic and important for every programmer. There will be some situations where a program or a particular block has to be executed only when a specific condition is True. These conditional statements will be very handy and fruitful in such situations. These conditional statements work as same as in other languages. However, syntax varies from language to language. The following are the conditional statements that we can use in MATLAB.
- if-end
- if-else-end
- nested-if-end
- if-elseif-elseif-else-end
- switch case
- nested switch case
if-end Statement
An if-end statement is the simplest decision-making statement. It decides whether a particular block of code has to be executed or not, based on the given boolean condition. Only when the given condition is true, it executes the statements inside the block otherwise not.
Syntax:
if (condition)
% statement(s) will execute
% if the boolean expression is true
<statements>
end
Example:
MATLAB
% MATLAB program to illustrate % if-end statement number = 28; if number>10 fprintf( 'The number is greater than 10.' ); end |
Output:
The number is greater than 10.
if-else-end statement
In conditional if Statement the additional block of code is merged as else statement which is performed when if the condition is false else condition won’t be executed when if the condition is True.
Syntax:
if (condition)
% statement(s) will execute
% if the boolean expression is true
<statement(s)>
else
<statement(s)>
% statement(s) will execute
% if the boolean expression is false
end
Example 1:
MATLAB
% MATLAB program to illustrate % if-else-end statement number = 28; if number<10 fprintf( 'The number is greater than 10' ); else fprintf( 'The number is not less than 10' ); end |
Output:
The number is not less than 10
Example 2: You can also chain if-else-end statements with more than one condition.
MATLAB
% MATLAB program to illustrate % if-else chaining number = 28; if number<10 fprintf( 'The number is less than 10\n' ); else if number<20 fprintf( 'The number is less than 20\n' ); else fprintf( 'The number is less than 30\n' ); end end |
Output:
The number is less than 30
Nested if-end Statement
There comes some situations where multiple conditions have to be satisfied to execute a block of code then we use nested if-end statements. This is nothing but another if condition(s) inside an if condition.
Syntax:
if (condition)
% Executes when the boolean expression 1 is true
if (condition)
% Executes when the boolean expression 2 is true
end
end
Example:
MATLAB
% MATLAB program to illustrate % Nested if-end statements number = 2; if number<10 fprintf( 'The number is less than 10\n' ); % Executes when then above if % condition is true if number<5 fprintf( 'Also The number is less than 5' ); end end |
Output:
The number is less than 10 Also The number is less than 5
if-elseif-elseif-else-end
An if statement can be followed by one (or more) optional elseif and an else statement, which is very useful to test various conditions.
Syntax:
if (condition)
% Executes when the expression 1 is true
<statement(s)>
elseif (condition)
% Executes when the boolean expression 2 is true
<statement(s)>
elseif (condition)
% Executes when the boolean expression 3 is true
<statement(s)>
else
% executes when the none of the above condition is true
<statement(s)>
end
Example:
MATLAB
% MATLAB program to illustrate % if-elseif-else-end statement number = 28; if number<10 fprintf( 'The number is less than 10\n' ); elseif number<20 fprintf( 'The number is less than 20\n' ); elseif number<30 fprintf( 'The number is less than 30\n' ); else fprintf( 'The number is less than 40\n' ); end |
Output:
The number is less than 30
Switch case
A switch block is familiar to if-elif-else-end statements. It works as same as in other languages except for syntax. But there are few key differences between them. A switch block conditionally executes one set of statements from several choices. Each choice is covered by a case statement. A switch expression can be any of the following.
- Numbers
- Characters
- Strings
- Objects
Here comparison of string expressions and case expressions is case-sensitive. Every character of a switch string expression must be matched with the case string expression.
Syntax:
switch (condition)
case condition
<statements>
case (condition)
<statements>
…
…
otherwise
<statements>
end
Example 1:
MATLAB
grade = 'A' ; switch (grade) case 'A' fprintf( 'Excellent!\n' ); case 'B' fprintf( 'Well done\n' ); case 'C' fprintf( 'Good\n' ); case 'D' fprintf( 'You passed\n' ); case 'F' fprintf( 'Better try again\n' ); otherwise fprintf( 'Invalid grade\n' ); end |
Output:
Excellent!
Example 2:
MATLAB
% MATLAB program to illustrate % String evaluation while using % switch case % First letter of the name was % given small letter intentionally name = 'w3wiki' switch (name) case 'w3wiki' fprintf( 'Hello from %s' ,name); otherwise fprintf( 'Not Matched!' ); end |
Output:
Not Matched!
Example 3:
MATLAB
% MATLAB program to illustrate % switch case days = 28; switch (days) case 28 fprintf( 'Normal Year!\n' ); otherwise fprintf( 'Leap Year\n' ); end |
Output:
Normal Year!
Nested Switch Case
This is similar to nested if statements. We can use switch as a part of the statement inside a switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.
Syntax:
switch (condition)
case (condition)
<statements>
switch (condition)
case (condition)
<statements>
…..
end
case (condition)
<statements>
end
Example: This example explains how the nested switch case and string comparison works in MATLAB.
MATLAB
% MATLAB program to illustrate % nested switch case days = 29; month = 'February' switch (days) case 29 fprintf( 'This Year is ' ); switch month case 'february' fprintf( 'Leap Year!\n' ); otherwise fprintf( 'Not Leap Year!\n' ); end end |
Output:
This Year is Not Leap Year!
Note:
- Unlike other programming languages, we don’t need to use break statement in switch case.
- Unlike other programming languages, we don’t use any kind of parentheses (i.e., (),{},: ) while writing conditions in conditional statements(i.e., if, nested if, etc). We do use end statement to end a conditional statement.
- Use semi-colons(;) wherever necessary to avoid unwanted outputs.
Contact Us