MATLAB – Conditional Statements
Last Updated :
26 Nov, 2020
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
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
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
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
number = 2;
if number<10
fprintf( 'The number is less than 10\n' );
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
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
name = 'geeksforGeeks'
switch (name)
case 'GeeksforGeeks'
fprintf( 'Hello from %s' ,name);
otherwise
fprintf( 'Not Matched!' );
end
|
Output:
Not Matched!
Example 3:
MATLAB
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
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.
Similar Reads
MATLAB - Break Statement
Break statement in MATLAB is used for breaking out of an iterative loop, for or while loop. The break in MATLAB is similar to the break statements in other programming languages such as C, C++, Python, etc. We will see how to break out of a single for or while loop and the nested implementation of t
2 min read
Switch case statement in Octave GNU
Octave is open-source, free available for many of the platforms. It is a high-level language. It comes up with a text interface along with an experimental graphical interface. It is also used for various Machine Learning algorithms for solving various numeric problems. You can say that it is similar
1 min read
Comments in MATLAB
Comments are generic English sentences, mostly written in a program to explain what it does or what a piece of code is supposed to do. More specifically, information that programmer should be concerned with and it has nothing to do with the logic of the code. They are completely ignored by the compi
2 min read
How to Use Logical Operator Within If Statements in MATLAB?
Logical Operators are used to combining two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a boolean value either true or false. Like any other programming language, logical operators in
4 min read
Nested Switch in MATLAB
Switch statements in MATLAB allow having multiple cases as conditionals. It is an implementation of the simple if-elif-else-end cycle but, better. In this article, we will see how nested switch statements work in MATLAB with the help of example. Syntax:switch choice_1 case <1> switch choice_2
2 min read
How to Use & and && Operator in MATLAB?
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of
3 min read
Loops (For and While) and Control Statements in Octave
Control statements are expressions used to control the execution and flow of the program based on the conditions provided in the statements. These structures are used to make a decision after assessing the variable. In this article, weâll discuss control statements like the if statement, for and whi
6 min read
How To Use | and || Operator in MATLAB?
In MATLAB, | and || are both logical operators that are used to perform logical OR operations on Boolean variables, however, there is a subtle difference between the two: |||Â The element-wise logical OR operator "|" takes two arrays of the same size and returns an array of the same size where each e
4 min read
Find() function in MATLAB
The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t
3 min read
If-Then-Else statement in SAS Programming
Comparison Operators used while using conditional statements. Symbol Mnemonic Meaning = EQ equals ^= or ~= NE not equal > GT greater than < LT less than >= GE greater than or equals <= LE less than or equals in IN selecting multiple values IF statement Syntax: IF (condition is true) =
3 min read