16
Conditionals 1. The if Statement 1

Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Embed Size (px)

Citation preview

Page 1: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Conditionals

1. The if Statement

1

Page 2: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

2

switch statement• Allows for evaluation of multiple cases of the

same variable

• The switch statement is looking for the variable to have an exact match to one of the cases. (No a<x && x<=b)

• 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 3: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

General Template

switch variablecase specification 1

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

<code block n>otherwise

<default block>end

3

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 4: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

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

4

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 5: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

5

Simulated “Run”

% Suppose 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 6: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Simulated “Run”

% Suppose 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

6

Page 7: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Simulated “Run”

% Suppose 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

7

Page 8: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Simulated “Run”

% Suppose 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

8

Page 9: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Simulated “Run”

% Suppose 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

9

Page 10: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Simulated “Run”

% Suppose 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

10

Page 11: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Simulated “Run”

% Suppose 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

1111

Page 12: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

Simulated “Run”

% Suppose 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

………

1212

Page 13: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

switch Example 2: strings

13

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 14: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

switch - Menu's

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

14

Page 15: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

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

15

Page 16: Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the

if versus switch

• As general ideas:

16

if switch

Combination of || statements that check equalityExample:

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: x==4 && y==7 √ preferred Impossible

Menus ok… √ preferred