Download presentation
Presentation is loading. Please wait.
Published byStephany Kelley Modified over 9 years ago
1
1. switch statement 2. “Nested” statements Conditionals, part 2 1
2
2. switch statement Allows for evaluation of multiple cases of the same parameter The switch statement is looking for the parameter to have an exact match to one of the cases. (No a<x && x<=b ) One case 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 parameter case specification1. case specification n otherwise end 3 There is no limit to the number of cases.
4
switch case: 1 st Example Let us review one of the previous example… 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,5,9,11} %30 days months days = 30; otherwise disp(‘Invalid Entry.’) end 4
5
switch case: 1 st Example Let us review one of the previous example… 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,5,9,11} %30 days months days = 30; otherwise disp(‘Invalid Entry.’) end 5 MUCH BETTER THAN if month==1 || month== 3 || month== 5 || … month== 7 || month== 8 || month== 10 || month== 12 Big advantage: reduces long OR statements of equality
6
switch case: 2 nd Example %ask user what he’d like to do menu_choice = input(‘Menu… (blablabla) 1 to 4: ’); %direct code to proper action switch menu_choice case 1 disp(‘You have selected 1.’) case {2,3,4} disp(‘You have selected a number from 2-4’) otherwise disp(‘Invalid Entry’) end 6
7
if versus. switch As general ideas: 7 ifswitch Combination of or statements that check equality Example: if x==1 || x==2 || x==3 || x==4 Ewwww…√ preferred Inequalities (, >)YesIMPOSSIBLE Conditions that check multiple variables Such as: if x==4 && y==7 √ preferredok Menusok√ preferred Conditions with &&YesIMPOSSIBLE
8
3. Nested statements “Nesting” means to encase one type of statement inside another. 8 For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end
9
3. Nested statements “Nesting” means to encase one type of statement inside another. 9 For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end Note that each if statement has its own end marker.
10
3. Nested statements “Nesting” means to encase one type of statement inside another. 10 For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end Note that each if statement has its own end marker.
11
3. Nested statements “Nesting” means to encase one type of statement inside another. 11 For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end Note that each if statement has its own end marker.
12
Nesting, cont. The if, elseif, else, and end keywords each mark the beginning and end of the code under its control. The elseif and else clauses of an if statement are optional. But you must include an end for every if statement – it marks the end of the code being executed conditionally. It can be helpful to comment lines with end keywords so that it is clear which statement is being terminated: end % of switch
13
Nesting, cont. Nesting can also mean using dissimilar statements – like nesting a switch within an if statement: if date > 20100101 %greater than January 1st 2010 switch month case {‘Dec’, ‘Jan’, ‘Feb’} disp(‘Winter’); case {‘Mar’, ‘Apr’, ‘May’} disp(‘Spring’); case {‘Jun’, ‘Jul’, ‘Aug’} disp(‘Summer’); case {‘Sep’, ‘Oct’, ‘Nov’} disp(‘Autumn’); end % of switch else disp(‘Date is too early’) end % of if date
14
Nesting, cont. In fact, you can “nest” any MATLAB code you want inside of if statements, switch statements, or even… Loops!
15
Wrapping Up switch statements only check EQUALITY So you'll never ever see any relational operators (> =..) If you do: you ARE definitely doing something wrong. TRUST US. They tremendously reduce if statements that check == with || statements. The are very easy to use with numbers AND strings We have not yet presented strings and if statements, since it is harder. Nested if/switch are entirely feasible. Make sure the end keywords are correctly placed. Ctrl+A+I will remind you what MATLAB sees. 15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.