1. Branching statements : if if else if elseif else and variations Consider sub6p0.m ? Is it a solution for the absolute value problem? A: No! For negative x it gives wrong answers! Consider sub6p0a.m ? What does it do A: Introduces an if statement in sub6p0.m to deal wit the negative x case. The default case (i.e. x>=0) is handled by the y=x; statement Everything else (x<0) by the if statement Consider sub6p0b.m ? What does it do A: Removes the default case by incorporating into into an if else statement The if part deals with x<0 but the else part is what used to be the default case (i.e. x>=0) Consider sub6p0c.m ? It is the previous sub6p0b.m but with an inline if-else statement. Note the use of semicolons! Consider sub6p0d.m ? Function abs does what sub6p0c.m can do. 2. Branching statement: if else end Consider sub6p10odd1.m It reads a number into x and determines whether for a digit x it is even or odd - Problems - What if x < 0 or x >9 i.e. x=-1 or x=13? - What if x is real number i.e. x=0.11 - Code is not robust. Consider sub6p10odd2.m It is more robust because it catches the case - What if x < 0 or x >9 i.e. x=-1 or x=13? But still fails to detect the other case. Consider sub6p10odd3.m - There are two if statements with the second one nested inside the else of the first if ! - The two pieces of code are sub6p10odd2.m and sub6p10odd3.m are equivalent but not identical - Consider what happens for x=8 - The former executes the fprintf(' even %d\n',x); and control next goes to the line following the end which has nothing and code terminates - In the latter sub6p10odd3.m we execute again fprintf(' even %d\n',x); and control next goes to the line following the first end. What is after the first end is a second end. Thus next (after going beyond the first end), control next goes to the statement following the second end which now has nothing and then code terminates. Thus in that latter case we executed twice the 'go to the statement after the end statement' whereas previously we executed this once. Even if the effect in both cases is the same (do nothing) it still cost some time to do nothing! 3. About to introduce the second branching stamement in MATLAB We use variable m to read a number between 1 and 12. We would like then to map that number into a month and print the number of days in that month. A solution for that is the following piece of MATLAB code sub6ex4.m m=input('Give month of year (Jan is 1 .. Dec is 12 '); if (m == 1 || m == 3 || m== 5 || m== 7 || m== 8 || m==10 || m==12 ) disp('31 days'); elseif (m== 4 || m==6 || m==9 || m==11) disp('30 days'); else disp('February'); end A switch solution to sub6ex4.m Is sub6ex4a.m month = input('input a number 1-12 for a month '); % File : sub6p5switchex.m switch (month) case 1 fprintf('31 \n'); case 2 fprintf(' February\n'); case 3 fprintf('31 \n'); case 4 fprintf('30 n'); case 5 fprintf('31 \n'); case 6 fprintf('30 n'); case 7 fprintf('31 \n'); case 8 fprintf('31 \n'); case 9 fprintf('30 \n'); case 10 fprintf('31 er \n'); case 11 fprintf('30 n'); case 12 fprintf('31 \n'); %otherwise % fprintf('Invalid month\n'); To make this code robust, we can uncomment the two line before end Syntax of switch from sub6p6switch.m % 1. General syntax of case statement File: sub6p6switch.m switch (switch_expression) case case_expr_1 Statement 1; Statement 2; case case_expr_2 Statement 3; Statement 4; otherwise Statement 5; Statement 6; end % 2. Several case statements can be combined together; if they execute the same % Statement 1, Statement 2 switch (switch_expression) case { case_expr_1 , case case_expr_2 } % These are braces! not parentheses in the case Statement 1; Statement 2; otherwise Statement 5; Statement 6; end A swith_expression can be a variable that takes a set of discrete values. If for a set of values a given action is taken, this set of values separated by commas and surround ed by curly braces { and } become part of a case clause. If that is the case the statement(zero or more) following the case all the way to the immediately following case or otherwise or end are executed and then control passes to the line after the end (of the switch). An otherwise statement catches all values not defined in case statements. 4. Refinement with switch sub6ex4 or sub6ex4a.m can be refined as in sub6ex5.m m=input('Give month of year (Jan is 1 .. Dec is 12 '); switch m case { 1, 3, 5, 7, 8, 10, 12} disp('31 days'); case { 4,6,9,11} disp('30 days'); otherwise disp('February'); end A more robust version is sub6ex5.m m=input('Give month of year (Jan is 1 .. Dec is 12 '); switch m case { 1, 3, 5, 7, 8, 10, 12} disp('31 days'); case { 4,6,9,11} disp('30 days'); case 2 disp('February'); otherwise disp('Invalid input'); end 5. Switch statement : more examples, Branching statement : try catch Consider month1.m Here month receives a value from 1-12 and we want to print the name of the month instead of figuring out how many days there are Note: code is not robust; an otherwise is missing to catch illegal values other than the integers from 1 to 12. Consider month2.m In this program we define a matrix monthlist. Note that it consists of 12 rows that are are strings of length 9 (the longest month in English is September with 9 characters). The matrix monthlist has geometry 12x9 and data type char (char array). monthlist(4,1) is 'A' and monthlist(4,2) is 'p' monthlist(4,1:end) is 'April' since end evaluates to 9 and thus 1:end means all characters starting from the 1st all the way to the last/9th. The case statements encompasses all legitimate values 1-12 and the otherwise catches illegal values for month. Consider month3.m In that program we remove the switch to our detriment a month equal to 13 generates a matrix dimensions error in RED INK on screen. Why ? Monthlist does not have a 13th column! How can we catch such error? For a start by using month2.m ! Or Consider month4.m The statements of month3.m are now enclosed in a try catch statement [ try catch end] try try lines to test for errors catch recovery from error end The statements between try and catch are the statements that MIGHT generate an error. Such an error will be caught by MATLAB without generating a RED INK message. In such a case a user can recover from the error or just print an informative message by having MATLAB execute the lines between the catch and end. So an error is caught and the line fprintf('\n\nException! Month value %d given as input is not in the range 1-12\n',month); is then executed! Note that this message is get printed whether an outof bounds error occurred in line month_name =monthlist(month,1:end); or somewhere else in the code between try and catch. One needs to be more thorough in the catch end section if one needs to figure out exactly what happened. 6. Branching Statements : A review note if else elseif end switch case otherwise end try catch end logical functions that can appear in control expressions isnumeric, isprime, ischar, isempty, isinf, isnan isletter, isspace, isa, islogical Examples isnumeric(5) % the numeric/number 5 but isnumeric('5') % the character 5 isprime(31) isprime(4) % prime numbers ischar('5') isempty(1:2:0) isinf(5/0) isnan(0/0) isa(5,'double') % Is 5 a double? sub6p0.m non solution to abs problem sub6p0a.m if statement sub6p0b.m if / else statement sub6p0c.m inline if /else sub6p0d.m abs function call sub6p4aif.m : Syntax IF sub6ex1.m : Weekday/Weekdn starting on Mon sub6ex2.m : Weekday/Weekdn starting on Sun sub6ex3.m : sub6ex4.m : Month sub6p10xy.m: Complicated example x,y sub6pextra.m: Complicated example sub6p10odd1.m : Error checking sub6p10odd1.m : Error checking sub6p10odd3.m : Nested IFs sub6p6switch.m : Syntax SWITCH sub6ex4.m : month if elseif else sub6ex4a.m : switch case simple sub6ex5.m : switch compact sub6ex6.m : compare to sub6ex5.m vote1.m vote3.m sub6p11oddswitch[123].m z.m : Be CAREFUL month1.m : print month SWITCH month2.m : array error checking month3.m : no switch month4.m : try catch Practice *********** type trickandtreat.m a=input('Give a new value for a '); b=input('Give a new value for b '); if (a < 10 & b> 10) if (a < 10 | b > 10) disp('Trick'); end disp('Treat'); end