180 likes | 189 Views
CS005 Introduction to Programming: Matlab. Eamonn Keogh eamonn@cs.ucr.edu. Conditional Branching. The if statement. Get the Users Score. Selectively run some code. Check if score less than 90. Yes. No. Display Warning Message. Continue…. Conditional Branching.
E N D
CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu
Conditional Branching The if statement Get the Users Score Selectively run some code Check if score less than 90 Yes No Display Warning Message Continue…
Conditional Branching The if/else statement Get the Users Score Selectively run one of two code options Check if score less than 90 No Yes Display Confirmation Message Display Warning Message Continue…
Conditional Branching The if/elseif statement Get the Users Score Selectively run one of three or more code options Check score Display A Display B Display C Display Fail Continue…
We can further generalize the if/else statement to include the elseifclause ifsome logical expression is true do these statements elseifsome other logical expression is true do these statements instead else do these statements instead end The if block The elseif block The else block In the above code, exactly one block will happen. There is no way that two or three could happen, and there is no way none could happen. This elseif statement divides the world into three mutually exclusive worlds
The last generalization of the if/elseifstatement is it realize that we can have more than one elseif ifsome logical expression is true do these statements elseifsome other logical expression is true do these statements instead elseifyet another logical expression is true do these statements instead else do these statements instead end The if block First elseif block Second elseif block The else block In the above code, exactly one block will happen. There is no way that more than one could happen, and there is no way none could happen. This elseif statement divides the world into three or more mutually exclusive worlds
Let us write a function that: • Asks a user to enter the grade they got out of 100 • Echoes the letter grade to the screen • Returns that grade (No input parameters) GetExamScore (echo letter grade) 79 Failing C B A 0 70 80 90 100 score
function Score = GetExamScore() Score = input('Enter your score : '); if Score >= 90 disp('A quality work'); elseif Score >= 80 disp('B quality work'); elseif Score >= 70 disp('C quality work'); else disp('Failing'); end end EDU>> EamonnsScore = GetExamScore() Enter your score : 99 A quality work EamonnsScore = 99
function Score = GetExamScore() Score = input('Enter your score : '); if Score >= 90 disp('A quality work'); elseif Score >= 80 disp('B quality work'); elseif Score >= 70 disp('C quality work'); elseifScore >= 0 disp('Failing'); end end Wrong! Common mistake The last block of code must be a else, not an elseif. (contrast with previous slide)
Let us write a function that: • Asks a user to enter their height in feet (no inches) • Echoes the {tall,medium,short} to the screen • Returns their height in feet (no inches) (No input parameters) If you are taller than 6 foot, you are tall If you are 5 foot, you are medium If you are 4 foot or shorter, you are short GetHeight (echo height) 4 Medium Short Tall 0 5 6 Height
function Height = GetHeight() % Has bug Height = input('Enter your height in feet only : '); if Height >= 6 disp('Tall'); elseif Height < 6 disp('Medium'); else disp('Short'); end end In this code it is never possible to be short, only tall or medium. function Height = GetHeight() Height = input('Enter your height in feet only : '); if Height >= 6 disp('Tall'); elseif Height >= 5 disp('Medium'); else disp('Short'); end end We need to test all the breakpoints (critical values), plus or minus 1
Medium Short Tall 0 5 6 Medium Short Tall if Height >= 6 0 5 6 Medium Short elseif Height >= 5 0 5 6 Short else 0 5
We can have if statements inside of if statements. This is called nesting. ifsome logical expression is true else do these statements instead end ifsome logical expression is true do these statements else do these statements instead end The if block The else block
Let us write a function that: • Asks a user to enter their sex and age • Returns a code, from one to four • 1 : man • 2 : woman • 3: boy • 4: girl • Echoes the persons label to the screen (No input parameters) EDU>> GetUserCode Enter 1 for male, 2 for female : 1 Enter your age : 55 Man EDU>> GetUserCode Enter 1 for male, 2 for female : 2 Enter your age : 5 Girl GetUserCode (echo person label) 4
function Label = GetUserCode() sex = input('Enter 1 for male, 2 for female : '); age = input('Enter your age : '); if sex == 1 disp('Male'); else disp('Female'); end end Let us build up to it. Let us ignore age for now, and ignore the return value. This code just correctly displays male/female EDU>> GetUserCode Enter 1 for male, 2 for female : 2 Enter your age : 2 Female EDU>>
function Label = GetUserCode() sex = input('Enter 1 for male, 2 for female : '); age = input('Enter your age : '); if sex == 1 if age >= 18 disp('Man'); else disp('Boy'); end else if age >= 18 disp('Woman'); else disp('Girl'); end end end Let us build up to it. Let us ignore the return value for now This code just correctly displays man/woman/boy/girl EDU>> GetUserCode Enter 1 for male, 2 for female : 1 Enter your age : 1 Boy EDU>> GetUserCode Enter 1 for male, 2 for female : 1 Enter your age : 55 Man EDU>> GetUserCode Enter 1 for male, 2 for female : 2 Enter your age : 2 Girl EDU>> GetUserCode Enter 1 for male, 2 for female : 2 Enter your age : 55 Woman EDU>>
function Label = GetUserCode() sex = input('Enter 1 for male, 2 for female : '); age = input('Enter your age : '); if sex == 1 if age >= 18 disp('Man'); Label = 1; else disp('Boy'); Label = 3; end else if age >= 18 disp('Woman'); Label = 2; else disp('Girl'); Label = 4; end end end The whole thing.
function Label = GetUserCode() sex = input('Enter 1 for male, 2 for female : '); age = input('Enter your age : '); if sex == 1 if age >= 18 disp('Man'); Label = 1; else disp('Boy'); Label = 3; end else if age >= 18 disp('Woman'); Label = 2; else disp('Girl'); Label = 4; end end end As an aside, we can begin to appreciate how important pretty printing is