Conditional Statements Switch Statements Why another conditional statement

  • Slides: 11
Download presentation
Conditional Statements Switch Statements

Conditional Statements Switch Statements

Why another conditional statement? § Multiple elseif statements can be included within an if

Why another conditional statement? § Multiple elseif statements can be included within an if statement answer = menu('What day is today? ', 'a) Mon', 'b) Tues', 'c) Wed', 'd) Thur', 'e) Fri'); if answer == 1 disp('Today is not Monday!'); elseif answer == 2 disp('Today is not Tuesday!'); elseif answer == 3 disp('That is correct!'); elseif answer == 4 disp('Today is not Thursday!'); elseif answer == 5 disp('Today is not Friday!'); else disp('Are you sure you entered a day? '); end

Switch Statements switch_variable case_expr 1 MATLAB® commands case_expr 2 MATLAB® commands … otherwise MATLAB®

Switch Statements switch_variable case_expr 1 MATLAB® commands case_expr 2 MATLAB® commands … otherwise MATLAB® commands end switch_variable is what controls which case is executed Each case compares the case_expr against the switch_variable to see if that case should be used If the case is selected, the MATLAB commands for that case will be executed If the switch_variable does not match any of the cases, the otherwise case will be executed

Switch Statements answer = menu('What day is today? ', 'a) Mon', 'b) Tues', 'c)

Switch Statements answer = menu('What day is today? ', 'a) Mon', 'b) Tues', 'c) Wed', 'd) Thur', 'e) Fri'); switch answer case 1 disp('Today is not Monday!'); case 2 disp('Today is not Tuesday!'); case 3 disp('That is correct!'); case 4 disp('Today is not Thursday!'); case 5 disp('Today is not Friday!'); otherwise disp('Are you sure you entered a day? '); end

Comments on Switch Statements § The case expressions can be almost any type of

Comments on Switch Statements § The case expressions can be almost any type of value available in MATLAB: § Number Ex. case 1 case 3. 14159 § Character Ex. case 'a' case '? ' § String Ex. case 'pizza' § Boolean Ex. case true case 'spam' case false § You cannot have a vector as a case expressions

Comments on Switch Statements § It is not necessary to include an otherwise statement

Comments on Switch Statements § It is not necessary to include an otherwise statement in a switch statement but it is generally a good idea Why? § Consider the following situation: You have written a MATLAB script to present the user with four images produced using a different amount of data. The purpose is to find out which picture is acceptable to the user in an attempt to use the least amount of data (and thus the least amount of memory).

Comments on Switch Statements % counts for user selections pic 1 = 0; pic

Comments on Switch Statements % counts for user selections pic 1 = 0; pic 2 = 0; pic 3 = 0; pic 4 = 0; % ask user to choose choice = input('Please select which picture is acceptable: '); % record the user’s choice switch choice case 1 pic 1 = pic 1 + 1; case 2 pic 2 = pic 2 + 1; case 3 pic 3 = pic 3 + 1; case 4 pic 4 = pic 4 + 1; end

Comments on Switch Statements % counts for user selections pic 1 = 0; pic

Comments on Switch Statements % counts for user selections pic 1 = 0; pic 2 = 0; pic 3 = 0; pic 4 = 0; % ask user to choose choice = input('Please select which picture is acceptable: '); % record the user’s choice switch choice case 1 pic 1 = pic 1 + 1; case 2 pic 2 = pic 2 + 1; case 3 pic 3 = pic 3 + 1; case 4 pic 4 = pic 4 + 1; otherwise disp('Incorrect selection'); end

Comments on Switch Statements § There may be situations where you want to same

Comments on Switch Statements § There may be situations where you want to same set of code to execute for several different cases answer = menu('What day is today? ', 'a) Mon', 'b) Tues', 'c) Wed', § Copy the code into each case: 'd) Thur', 'e) Fri'); answer = menu('What day is today? ', 'a) Mon', 'b) Tues', 'c) Wed', switch answer § Combine multiple cases into a single 'd) case: Thur', 'e) Fri'); case menu('What 1 answer day is today? ', 'a) Mon', 'b) Tues', 'c) Wed', switch = answer disp('Today is not Monday!'); 'd) Thur', 'e) Fri'); case 1 case 2 switch answer disp('That is not correct!'); is not Tuesday!'); casedisp('Today {1, 2, 4, 5} 2 casedisp('That 3 is not correct!'); disp('That is correct!'); case 3 casedisp('That 4 is correct!'); disp('Today is not Thursday!'); otherwise case 4 casedisp('Are 5 you entered a day? '); disp('Thatyou is sure not correct!'); disp('Today is not Friday!'); end case 5 otherwise disp('That is not correct!'); disp('Are you sure you entered a day? '); otherwise § Multiple cases can be combined by placing them inside end disp('Are you sure you entered a day? '); curly brackets {case_expr 1, case_expr 2, …} end

switch vs. if § With multiple conditional statements available, we need to be able

switch vs. if § With multiple conditional statements available, we need to be able to decide when to use them Use an if when… Use a switch when… § You have a range of values § You have a specific set of values you need to test § All of your conditions can be satisfied with == comparisons § You only have one variable you need to check x<5 § You have complex conditions x < 5 && x > 2 § Your condition uses multiple variables x > 10 && y < 5 § You can always use an if statement § You should only use a switch with a finite set of possible values

Test Your Understanding

Test Your Understanding