1 / 22

Conditionals, part 2

Conditionals, part 2. the switch statement. General Concepts of Conditionals (revisit). You may want to execute some part of code under certain circumstances only You may want to skip some part of a code You may want to repeat a section of code until a new circumstance happens

isi
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 the switchstatement

  2. General Concepts of Conditionals (revisit) • You may want to execute some part of code under certain circumstances only • You may want to skip some part of a code • You may want to repeat a section of code until a new circumstance happens • You may want to repeat a section of code for a certain number of times

  3. if-elseif-else Review Write a section of code that will assign the number of days in a given month to a variable Thirty days hath September, April, June, and November. All the rest have thirty-one, Excepting February alone, And that has twenty-eight days clear, And twenty-nine in each leap year

  4. if-elseif-else Review Write a section of code that will assign the number of days in a given month to a variable MATLAB code: %Request user to enter the month number (Jan=1, Aug=8) month = input(‘Enter the month number: ‘); %if months are jan,mar,may,jul,aug,oct,dec if month==1 || month==3 || month==5 || month==7 || … month==8 || month==10 || month==12 nb_days = 31; elseif month == 2 %February nb_days = 29; % for leap years… else %every other months nb_days = 30; end

  5. if-elseif-else Review %if months are jan,mar,may,jul,aug,oct,dec if month==1 || month==3 || month==5 || month==7 || … month==8 || month==10 || month==12 nb_days = 31; elseif month == 2 %February nb_days = 29; % for leap years… else %every other months nb_days = 30; end What are some characteristics of this code segment? What are its limitations?

  6. switchstatement • 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.

  7. General Template switchparameter casespecification 1 <code block 1> . . . . casespecification n <code block n> otherwise <default block> end if <condition 1> <code block 1> elseif<condition 2> <code block 2> . . elseif<condition n> <code block n> There is no limit to the number of cases. else <default block> end

  8. switch Example 1: Multiple Cases • Let us modify the calendar example from an if to a switch • ifmonth==1 || month== 3 || month== 5 || … • month== 7 || month== 8 || month== 10 || month== 12 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 Big advantage: reduces long OR statements of equality

  9. Simulated “Run” %assume 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. switch Example 2: strings • 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

  11. switch - Menu’s • Several programs request the user to select an item from a menu:

  12. switchExample 3: Menu Options %ask user what he’d like to do menu_choice = input(‘Select Item 1 to 4: ’); %direct code to proper action switchmenu_choice case1 fprintf(‘You have selected 1.\n’) case2 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

  13. ifversus switch • As general ideas:

  14. Nesting Conditionals “Nested” statements

  15. Nestedstatements “Nesting” means to encase one type of statement inside another. For example: Nestedifstatements length = input(‘Length (m), please: ’); iflength > 0 diameter = input(‘Diameter (m), please: ’); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end end Note that each if statement has its own end marker.

  16. Nested statement – Example 1 % assume user will enter -2 for length and % -5 for diameter length = input(‘Length (m), please: ’); if length > 0 diameter = input(‘Diameter (m), please: ’); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end end

  17. Nested statement – Example • How is this code different? length = input(‘Length (m), please: ’); diameter = input(‘Diameter (m), please: ’); iflength > 0 && diameter > 0 volume = pi * (diameter / 2)^2 * length; fprintf(‘Volume = %.3f m^3\n’,volume); else <error message> end …… Asks for both inputs without checking

  18. Nested Statements The if, elseif, else, and end keywords each mark the beginning and end of the code under its control. The elseif and elseare 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 end % positive length

  19. Nested statements: Practice 1 • What is the result? x = x + y - z; ifx>0 && z<0 || y<0 y = 0; else switchx case 2 fprintf(‘x is now 2’) case 5 fprintf(‘x is now 5’) otherwise fprintf(‘ERROR somewhere’) end end

  20. Nested statements: Practice 2 • What is the result? x = x + y - z; ifx>0 && z<0 || y<0 y = 0; else switchx case 2 fprintf(‘x is now 2’) case 5 fprintf(‘x is now 5’) otherwise fprintf(‘ERROR somewhere’) end end -

  21. What is wrong with this code? x = x + y – z; ifx>0 && z<0 || y<0 y = 0; else switchx case2 fprintf(‘x is now 2’) case5 fprintf(‘x is now 5’) otherwise fprintf(‘ERROR somewhere’) end end ………. Pay attention to indentation!

  22. Key Ideas • Switch statements allow you to evaluate multiple cases of same parameter • The cases can have unlimited values, but they must be equivalencies • Switch statements are well suited for menu selection • and string comparisons • Nesting can be used to skip code that requires additional conditional statements • Any MATLAB code can be nested inside of if statements, switch statements, or loops (while, for)

More Related