Download presentation
Presentation is loading. Please wait.
Published byMorgan Bruce Modified over 9 years ago
1
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. 2
3
General Template switch variable case specification 1. case specification n otherwise end 3 if elseif. elseif else end There is no limit to the number of cases.
4
switch Example 1: Multiple Cases Instead we use… switch month case {1,3,5,7,8,10,12} % 31-day months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-day months days = 30; otherwise fprintf('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 month case {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 5
6
Simulated “Run” % Suppose month is 4 switch month case {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
7
Simulated “Run” % Suppose month is 4 switch month case {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
8
Simulated “Run” % Suppose month is 4 switch month case {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
9
Simulated “Run” % Suppose month is 4 switch month case {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
10
Simulated “Run” % Suppose month is 4 switch month case {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
11
Simulated “Run” % Suppose month is 4 switch month case {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 11
12
Simulated “Run” % Suppose month is 4 switch month case {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 ……… 12
13
switch Example 2: strings 13 switch statements can also be used to evaluate strings month = input('Enter the month: ', 's') switch month case {'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
14
switch - Menu's Several programs request the user to select an item from a menu: 14
15
switch Example 3: Menu Options %ask user what he'd like to do menu_choice = input('Select Item 1 to 4: '); %direct code to proper action switch menu_choice case 1 fprintf('You have selected 1.\n') case 2 fprintf('You have selected a number 2.\n') case 3 fprintf('You have selected a number 3.\n') case 4 fprintf('You have selected a number 4.\n') otherwise fprintf('Invalid Entry.\n'); end 15
16
if versus switch As general ideas: 16 ifswitch Combination of || statements that check equality Example: x==1 || x==2 || x==3 || x==4 Yes√ preferred Inequalities ( =, >)YesImpossible Conditions with &&: x == 2 && x == 7 YesImpossible Conditions that check multiple variables Such as: x==4 && y==7 √ preferredImpossible Menusok…√ preferred
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.