MATLAB Switch Statement switch Switch among several cases

advertisement
MATLAB Switch Statement
switch Switch among several cases based on expression.
The general form of the switch statement is:
switch switch_expr
CASE case_expr,
statement, ..., statement
CASE {case_expr1, case_expr2, case_expr3,...}
statement, ..., statement
...
OTHERWISE,
statement, ..., statement
END
In its simplest form, the switch-expr is a scalar variable. When the value of switch_expr matches one of the
case_expr values in the CASE statements, the statements in that CASE block are executed, then execution
continues following the END statement.
When a CASE block is executed, execution continues following the END statement, i.e., the other CASE blocks
are skipped.
If none of the case_expr values match the switch_expr, the statements in the OTHERWISE block are executed,
then execution continues following the END statement.
Example:
% LanternSwitch.m
%
% This is the same simple example done earlier with IF
%
% Give 1 or 2 lanterns in church tower, prints "by land", "by sea", or "you
% must enter either 1 or 2".
%
% Input: a single number
%
% Output: one of phrases listed above, depending on input
%
% Author: J Reising
30 Jan 2014
a = input('Enter the number of lanterns hanging in the church tower
');
switch a
case 1
fprintf('\n\nBy Land\n\n')
case 2
fprintf('\n\nBy Sea\n\n')
otherwise
fprintf('\n\nYou must enter either 1 or 2\n\n')
end
Try commenting out the otherwise block, entering something other than a 1 or a 2, and see what happens.
The same example written to use string values for the variable used to switch:
% LanternSwitchText.m
%
% This is the same simple example done earlier with IF
%
% Give 1 or 2 lanterns in church tower, prints "by land", "by sea", or "you
% must enter either 'one' or 'two'".
%
% Input: a single word
%
% Output: one of phrases listed above, depending on input
%
% Author: J Reising
3 Feb 2014
a = input('Enter the number of lanterns hanging in the church tower
');
switch a
case 'one'
fprintf('\n\nBy Land\n\n')
case 'two'
fprintf('\n\nBy Sea\n\n')
otherwise
fprintf('\n\nYou must enter either ''one'' or ''two''\n\n')
end
This program could be altered to disregard the case (upper or lower) of the letters by using switch lower(a).
The case_expr values could be selected from a set of values by using MATLAB set notation by enclosing the
members of the set in { } as in the following example:
% EvenOrOdd.m
%
% This is a simple example of using sets of values for the swith_expr in
% the switch statement.
%
% Input: a non-negative integer less than or equal to 10
%
% Output: 'Even', 'Odd', or 'You must enter a non-negative integer less
% than or equal to 10'
%
% Author: J Reising 3 Feb 2014
%
n = input('Enter a non-negative integer <= 10
');
switch n
case {0,2,4,6,10}
fprintf('\n\nEven\n\n')
case {1,3,5,7,9}
fprintf('\n\nOdd\n\n')
otherwise
fprintf('\n\nYou did not enter a non-negative integer <= 10\n\n')
end
As an in-class exercise, change the Quadratic.m script to a version using the switch statement instead of IF
statements. Note that there are either 0, 1, or 2 roots. If you examine the coefficients and the discriminant,
you should be able to use a variable called, for example, numroots as the switch_expr.
Download