Decisions


>> Decision making structures require that the programmer specify one or more conditions to be evaluated by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, if the condition is determined to be false other statements to be executed.


MATLAB provides following types of decision making statements.


1. if ... end statement.


2. if...else...end statement.


3. If... elseif...elseif...else...end statements.


4. nested if statements.


5. switch statement.


6. nested switch statements




1. if ... end statement.


An if ... end statement consists of an if statement and a boolean expression followed by one or more statements. It is delimited by the end statement.


Syntax:


The syntax of an if statement in MATLAB is given below. If the expression evaluates to true, then the block of code inside the if statement will be executed. If the expression evaluates to false, then the first set of code after the end statement will be executed.


if <expression>


% statement(s) will execute if the boolean expression is true


<statements>

end


Example:






2. if...else...end statement.


An if statement can be followed by an optional else statement, which executes when the expression is false. If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.



Syntax:


The syntax of an if...else statement in MATLAB is:


if <expression>


% 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:





3. If... elseif...elseif...else...end statements.


An if statement can be followed by an (or more) optional elseif... and an else statement, which is very useful to test various condition.


When using if... elseif...else statements, there are few points to keep in mind:


a. An if can have zero or one else's and it must come after any elseif's.


b. An if can have zero to many elseif's and they must come before the else.



c. Once an else if succeeds, none of the remaining elseif's or else's will be tested.

Syntax:


if <expression 1>


% Executes when the expression 1 is true 


<statement(s)>


elseif <expression 2>


% Executes when the boolean expression 2 is true


<statement(s)>


Elseif <expression 3>


% 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:






4. nested if statements:


It is always legal in MATLAB to nest if-else statements which means you can use one if or elseif statement inside another if or elseif statement(s).


Syntax:



The syntax for a nested if statement is as follows:

if <expression 1>


% Executes when the boolean expression 1 is true 


if <expression 2>


% Executes when the boolean expression 2 is true

    
end


end


Example:






5. switch statement:


A switch block conditionally executes one set of statements from several choices. Each choice is covered by a case statement.


>> An evaluated switch_expression is a scalar or string.


>> An evaluated case_expression is a scalar, a string or a cell array of scalars or strings.


The switch block tests each case until one of the cases is true. A case is true when:


>> For numbers, eq(case_expression,switch_expression).


>> For strings, strcmp(case_expression,switch_expression).


>> For objects that support the eq function, eq(case_expression,switch_expression).


>> For a cell array case_expression, at least one of the elements of the cell array matches switch_expression, as defined above for numbers, strings and objects.


When a case is true, MATLAB executes the corresponding statements and then exits the switch block.


The otherwise block is optional and executes only when no case is true.


Syntax:



The syntax of switch statement in MATLAB is:

switch <switch_expression>


case <case_expression>


<statements>


case <case_expression>


<statements>

     ...
     ...

otherwise


<statements>



end


Example:






6. nested switch statements:


It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.


Syntax:


The syntax for a nested switch statement is as follows:


switch(ch1) 


case 'A' 


fprintf('This A is part of outer switch');


switch(ch2) 


case 'A'


fprintf('This A is part of inner switch' );


case 'B'  


fprintf('This B is part of inner switch' );


end   


case 'B'


fprintf('This B is part of outer switch' );



end


Example: