Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS005 Introduction to Programming:

Similar presentations


Presentation on theme: "CS005 Introduction to Programming:"— Presentation transcript:

1 CS005 Introduction to Programming:
Matlab Eamonn Keogh

2 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…

3 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…

4 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…

5 We can further generalize the if/else statement to include the elseif clause
if some logical expression is true do these statements elseif some other logical expression is true do these statements instead else 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

6 The last generalization of the if/elseif statement is it realize that we can have more than one elseif if some logical expression is true do these statements elseif some other logical expression is true do these statements instead elseif yet another logical expression is true else 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

7 GetExamScore Let us write a function that: Failing C B A
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 70 80 90 100 score

8 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 EDU>> EamonnsScore = GetExamScore() Enter your score : 99 A quality work EamonnsScore = 99

9 Wrong! 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'); elseif Score >= 0 disp('Failing'); end Wrong! Common mistake The last block of code must be a else, not an elseif. (contrast with previous slide)

10 GetHeight Let us write a function that: Short Tall
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 5 6 Height

11 In this code it is never possible to be short, only tall or medium.
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 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 We need to test all the breakpoints (critical values), plus or minus 1

12 Tall Short Short Tall Short Short Medium Medium Medium 6 5
Tall 6 5 Medium Short Medium Short Tall if Height >= 6 5 6 Medium Short elseif Height >= 5 5 6 Short else 5

13 We can have if statements inside of if statements
We can have if statements inside of if statements. This is called nesting. if some logical expression is true else do these statements instead end if some logical expression is true do these statements else do these statements instead end The if block The else block

14 GetUserCode 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 Enter 1 for male, 2 for female : 2 Enter your age : 5 Girl GetUserCode (echo person label) 4

15 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 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>>

16 Let us ignore the return value for now
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 disp('Woman'); disp('Girl'); 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 Enter your age : 55 Man Enter 1 for male, 2 for female : 2 Enter your age : 2 Girl Woman EDU>>

17 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 disp('Woman'); Label = 2; disp('Girl'); Label = 4; The whole thing.

18 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 disp('Woman'); Label = 2; disp('Girl'); Label = 4; As an aside, we can begin to appreciate how important pretty printing is


Download ppt "CS005 Introduction to Programming:"

Similar presentations


Ads by Google