VOOZH about

URL: https://www.geeksforgeeks.org/matlab/matlab-conditional-statements/

⇱ MATLAB - Conditional Statements - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:

 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:

 Output:

The number is not less than 10

Example 2: You can also chain if-else-end statements with more than one condition.

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:

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:

 
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:

 
Output:

Excellent!

Example 2: 

Output:

Not Matched!

Example 3:

 
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.

Output:

This Year is Not Leap Year!

Note:

  1. Unlike other programming languages, we don't need to use break statement in switch case.
  2. 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.
  3. Use semi-colons(;) wherever necessary to avoid unwanted outputs.
Comment