1 / 11

Conditionals, part 2

Conditionals, part 2. switch statement “Nested” statements. 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 )

anja
Download Presentation

Conditionals, part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Conditionals, part 2 switch statement “Nested” statements

  2. 2. switch statement • Allows for evaluation of multiple cases of the sameparameter • The switch statement is looking for the parameter to have an exact matchto 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.

  3. General Template switchparameter casespecification1 <code block> . . . casespecification n <code block n> otherwise <execute a default block> end There is no limit to the number of cases.

  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 ifmonth==1 || month== 3 || month== 5 || … month== 7 || month== 8 || month== 10 || month== 12 4

  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:

  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 Note that each if statement has its own end marker.

  8. 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

  9. 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

  10. Nesting, cont. • In fact, you can “nest” any MATLAB code you want inside of if statements, switch statements, or even… Loops!

  11. 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.

More Related