Decision making structures require that the programmer should specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the programming languages −
MATLAB provides following types of decision making statements. Click the following links to check their detail −
MATLAB - 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 −
if <expression> % statement(s) will execute if the boolean expression is true <statements> end
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.
Flow Diagram
Example
Create a script file and type the following code −
Live Demoa = 10; % check the condition using if statement if a < 20 % if condition is true then print the following fprintf('a is less than 20\n' ); end fprintf('value of a is : %d\n', a);
When you run the file, it displays the following result −
a is less than 20 value of a is : 10
An if statement can be followed by an optional else statement, which executes when the expression is false.
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
If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
Flow Diagram
Example
Create a script file and type the following code −
Live Demoa = 100; % check the boolean condition if a < 20 % if condition is true then print the following fprintf('a is less than 20\n' ); else % if condition is false then print the following fprintf('a is not less than 20\n' ); end fprintf('value of a is : %d\n', a);
When the above code is compiled and executed, it produces the following result −
a is not less than 20 value of a is : 100
An if statement can be followed by one (or more) optional elseif... and an else statement, which is very useful to test various conditions.
When using if... elseif...else statements, there are few points to keep in mind −
- An if can have zero or one else's and it must come after any elseif's.
- An if can have zero to many elseif's and they must come before the else.
- 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
Create a script file and type the following code in it −
Live Demoa = 100; %check the boolean condition if a == 10 % if condition is true then print the following fprintf('Value of a is 10\n' ); elseif( a == 20 ) % if else if condition is true fprintf('Value of a is 20\n' ); elseif a == 30 % if else if condition is true fprintf('Value of a is 30\n' ); else % if none of the conditions is true ' fprintf('None of the values are matching\n'); fprintf('Exact value of a is: %d\n', a ); end
When the above code is compiled and executed, it produces the following result −
None of the values are matching Exact value of a is: 100
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
You can nest elseif...else in the similar way as you have nested if statement.
Example
Create a script file and type the following code in it −
Live Demoa = 100; b = 200; % check the boolean condition if( a == 100 ) % if condition is true then check the following if( b == 200 ) % if condition is true then print the following fprintf('Value of a is 100 and b is 200\n' ); end end fprintf('Exact value of a is : %d\n', a ); fprintf('Exact value of b is : %d\n', b );
When you run the file, it displays −
Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
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(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
Create a script file and type the following code in it −
Live Demograde = 'B'; switch(grade) case 'A' fprintf('Excellent!\n' ); case 'B' fprintf('Well done\n' ); case 'C' fprintf('Well done\n' ); case 'D' fprintf('You passed\n' ); case 'F' fprintf('Better try again\n' ); otherwise fprintf('Invalid grade\n' ); end
When you run the file, it displays −
Well done
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
Create a script file and type the following code in it −
Live Demoa = 100; b = 200; switch(a) case 100 fprintf('This is part of outer switch %d\n', a ); switch(b) case 200 fprintf('This is part of inner switch %d\n', a ); end end fprintf('Exact value of a is : %d\n', a ); fprintf('Exact value of b is : %d\n', b );
When you run the file, it displays −
This is part of outer switch 100 This is part of inner switch 100 Exact value of a is : 100 Exact value of b is : 200
No comments:
Post a Comment