Conditionals, part 2 1. switch statement 2. “Nested” statements 1 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 General Template switch parameter case specification1 <code block> . . There is no limit to the . number of cases. case specification n <code block n> otherwise <execute a default block> end 3 switch case: 1st 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 switch case: 1st 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 Big advantage: reduces long OR statements of equality MUCH BETTER THAN if month==1 || month== 3 || month== 5 || … month== 7 || month== 8 || month== 10 || month== 12 5 switch case: 2nd 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 if versus. switch As general ideas: if Combination of or statements that check equality Example: if x==1 || x==2 || x==3 || x==4 Ewwww… Inequalities (<, <=, =>, >) Conditions that check multiple variables Such as: if x==4 && y==7 Yes √ switch √ preferred IMPOSSIBLE preferred ok Menus ok √ preferred Conditions with && Yes IMPOSSIBLE 7 3. Nested statements “Nesting” means to encase one type of statement inside another. For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end end 8 3. Nested statements “Nesting” means to encase one type of statement inside another. For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end end Note that each if statement has its own end marker. 9 3. Nested statements “Nesting” means to encase one type of statement inside another. For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end 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. For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end end Note that each if statement has its own end marker. 11 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 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 Nesting, cont. In fact, you can “nest” any MATLAB code you want inside of if statements, switch statements, or even… Loops! 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. They tremendously reduce if statements that check == with || statements. The are very easy to use with numbers AND strings TRUST US. 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