Date post: | 21-Jan-2016 |
Category: |
Documents |
Upload: | morgan-bruce |
View: | 214 times |
Download: | 0 times |
Conditionals
1. The if Statement
1
2
switch statement• Allows for evaluation of multiple cases of the
same variable
• The switch statement is looking for the variable to have an exact match to one of the cases. (No a<x && x<=b)
• Specification may have multiple values enclosed in braces {…}
• The default case catches any values of the parameter other than the specified cases.
• The default case should trap bad parameter values.
General Template
switch variablecase specification 1
<code block 1>....case specification n
<code block n>otherwise
<default block>end
3
if <condition 1>
<code block 1>
elseif <condition 2> <code block 2>..
elseif <condition n> <code block n>else <default block>end
There is no limit to the number of cases.
switch Example 1: Multiple Cases
Instead we use…switch month
case {1,3,5,7,8,10,12} % 31-day monthsdays = 31;
case 2days = 29; % leap year to be coded..
case {4,6,9,11} % 30-day monthsdays = 30;
otherwisefprintf('Invalid Entry.\n');
end
4
Let us modify the calendar example from an if to a switch
if month==1 || month== 3 || month== 5 || …month== 7 || month== 8 || month== 10 || month== 12
Big advantage: reduces long OR statements of equality
5
Simulated “Run”
% Suppose month is 4
switch monthcase {1,3,5,7,8,10,12} % 31-days months
days = 31;case 2
days = 29; % leap year to be coded..case {4,6,9,11} % 30-days months
days = 30; otherwise
fprintf('Invalid Entry.\n');end
Simulated “Run”
% Suppose month is 4
switch monthcase {1,3,5,7,8,10,12} % 31-days months
days = 31;case 2
days = 29; % leap year to be coded..case {4,6,9,11} % 30-days months
days = 30; otherwise
fprintf('Invalid Entry.\n');end
6
Simulated “Run”
% Suppose month is 4
switch monthcase {1,3,5,7,8,10,12} % 31-days months
days = 31;case 2
days = 29; % leap year to be coded..case {4,6,9,11} % 30-days months
days = 30; otherwise
fprintf('Invalid Entry.\n');end
7
Simulated “Run”
% Suppose month is 4
switch monthcase {1,3,5,7,8,10,12} % 31-days months
days = 31;case 2
days = 29; % leap year to be coded..case {4,6,9,11} %30-days months
days = 30; otherwise
fprintf('Invalid Entry.\n');end
8
Simulated “Run”
% Suppose month is 4
switch monthcase {1,3,5,7,8,10,12} % 31-days months
days = 31;case 2
days = 29; % leap year to be coded..case {4,6,9,11} % 30-days months
days = 30; otherwise
fprintf('Invalid Entry.\n');end
9
Simulated “Run”
% Suppose month is 4
switch monthcase {1,3,5,7,8,10,12} % 31-days months
days = 31;case 2
days = 29; % leap year to be coded..case {4,6,9,11} % 30-days months
days = 30; otherwise
fprintf('Invalid Entry.\n');end
10
Simulated “Run”
% Suppose month is 4
switch monthcase {1,3,5,7,8,10,12} % 31-days months
days = 31;case 2
days = 29; % leap year to be coded..case {4,6,9,11} % 30-days months
days = 30; otherwise
fprintf('Invalid Entry.\n');end
1111
Simulated “Run”
% Suppose month is 4
switch monthcase {1,3,5,7,8,10,12} % 31-days months
days = 31;case 2
days = 29; % leap year to be coded..case {4,6,9,11} % 30-days months
days = 30; otherwise
fprintf('Invalid Entry.\n');end
………
1212
switch Example 2: strings
13
switch statements can also be used to evaluate stringsmonth = input('Enter the month: ', 's')
switch monthcase {'Jan','March','May','July'... } %31-days -days = 31;case 'Feb'
days = 29; %leap year to be coded..case {'April', 'June','Sept','Nov'} %30-days
days = 30; otherwise
fprintf('Invalid Entry.\n');end
switch - Menu's
• Several programs request the user to select an item from a menu:
14
switch Example 3: Menu Options %ask user what he'd like to domenu_choice = input('Select Item 1 to 4: ');
%direct code to proper actionswitch menu_choice
case 1fprintf('You have selected 1.\n')
case 2fprintf('You have selected a number 2.\n')
case 3fprintf('You have selected a number 3.\n')
case 4fprintf('You have selected a number 4.\n')
otherwisefprintf('Invalid Entry.\n');
end
15
if versus switch
• As general ideas:
16
if switch
Combination of || statements that check equalityExample:
x==1 || x==2 || x==3 || x==4 Yes √ preferred
Inequalities (<, <=, >=, >) Yes Impossible
Conditions with &&: x == 2 && x == 7 Yes Impossible
Conditions that check multiple variablesSuch as: x==4 && y==7 √ preferred Impossible
Menus ok… √ preferred