conditionals, part 2

22
Conditionals, part 2 1. the switch statement 1

Upload: isi

Post on 23-Feb-2016

53 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Conditionals, part 2

1

Conditionals, part 2

1. the switch statement

Page 2: Conditionals, part 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

2

Page 3: Conditionals, part 2

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

if-elseif-else Review

3

Page 4: Conditionals, part 2

Write a section of code that will assign the number of days in a given month to a variable

MATLAB code:

if-elseif-else Review

%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,decif month==1 || month==3 || month==5 || month==7 || …

month==8 || month==10 || month==12nb_days = 31;

elseif month == 2 %Februarynb_days = 29; % for leap years…

else %every other monthsnb_days = 30;

end 4

Page 5: Conditionals, part 2

%if months are jan,mar,may,jul,aug,oct,decif month==1 || month==3 || month==5 || month==7 || …

month==8 || month==10 || month==12nb_days = 31;

elseif month == 2 %Februarynb_days = 29; % for leap years…

else %every other monthsnb_days = 30;

end

What are some characteristics of this code segment?

What are its limitations?

if-elseif-else Review

5

Page 6: Conditionals, part 2

6

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.

Page 7: Conditionals, part 2

General Template

switch parametercase specification 1

<code block 1>....case specification n

<code block n>otherwise

<default block>end

7

if <condition 1>

<code block 1>

elseif <condition 2> <code block 2>..elseif <condition n> <code block n>else <default block>end

There is no limit to the number of cases.

Page 8: Conditionals, part 2

switch Example 1: Multiple Cases

Instead we use…switch month

case {1,3,5,7,8,10,12} %31-day monthsdays = 31;case 2days = 29; %leap year to be coded..case {4,6,9,11} %30-day monthsdays = 30; otherwisefprintf(‘Invalid Entry.\n’);

end

8

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

Page 9: Conditionals, part 2

9

Simulated “Run”

%assume month is 4

switch monthcase {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

Page 10: Conditionals, part 2

switch Example 2: strings

10

switch statements can also be used to evaluate stringsmonth = input(‘Enter the month: ‘, ‘s’)

switch monthcase {‘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

Page 11: Conditionals, part 2

switch - Menu’s

• Several programs request the user to select an item from a menu:

11

Page 12: Conditionals, part 2

switch Example 3: Menu Options

%ask user what he’d like to domenu_choice = input(‘Select Item 1 to 4: ’);

%direct code to proper actionswitch menu_choice

case 1fprintf(‘You have selected 1.\n’)

case 2fprintf(‘You have selected a number 2.\n’)

case 3fprintf(‘You have selected a number 3.\n’)

case 4fprintf(‘You have selected a number 4.\n’)

otherwisefprintf(‘Invalid Entry.\n’);

end

12

Page 13: Conditionals, part 2

if versus switch

• As general ideas:

13

if switch

Combination of || statements that check equalityExample:

if x==1 || x==2 || x==3 || x==4 Yes √ preferred

Inequalities (<, <=, >=, >) Yes Impossible

Conditions with &&: x == 2 && x == 7 Yes Impossible

Conditions that check multiple variablesSuch as: if x==4 && y==7 √ preferred Impossible

Menus ok… √ preferred

Page 14: Conditionals, part 2

14

Nesting Conditionals

1. “Nested” statements

Page 15: Conditionals, part 2

Nested statements

15

“Nesting” means to encase one type of statement inside another.

For example: Nested if statements

length = input(‘Length (m), please: ’);if length > 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.

Page 16: Conditionals, part 2

% 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

Nested statement – Example 1

16

Page 17: Conditionals, part 2

Nested statement – Example

17

length = input(‘Length (m), please: ’);diameter = input(‘Diameter (m), please: ’);if length > 0 && diameter > 0

volume = pi * (diameter / 2)^2 * length;fprintf(‘Volume = %.3f m^3\n’,volume);

else<error message>

end……

• How is this code different?

Asks for both inputs without checking

Page 18: Conditionals, part 2

Nested Statements

The if, elseif, else, and end keywords each mark the beginning and end of the code under its control.

18

The elseif and else 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 switchend % positive length

Page 19: Conditionals, part 2

Nested statements: Practice 1

x = x + y - z;if x>0 && z<0 || y<0

y = 0;else

switch xcase 2

fprintf(‘x is now 2’)case 5

fprintf(‘x is now 5’)otherwise

fprintf(‘ERROR somewhere’)end

end

19

• What is the result?

Page 20: Conditionals, part 2

Nested statements: Practice 2

x = x + y - z;if x>0 && z<0 || y<0

y = 0;else

switch xcase 2

fprintf(‘x is now 2’)case 5

fprintf(‘x is now 5’)otherwise

fprintf(‘ERROR somewhere’)end

end

20

• What is the result?

-

Page 21: Conditionals, part 2

What is wrong with this code?

x = x + y – z; if x>0 && z<0 || y<0y = 0; elseswitch x

case 2fprintf(‘x is now 2’)

case 5fprintf(‘x is now 5’)otherwise

fprintf(‘ERROR somewhere’)endend

……….

21

Pay attention to indentation!

Page 22: Conditionals, part 2

• 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)

Key Ideas

22