Download presentation
Presentation is loading. Please wait.
1
CS005 Introduction to Programming: Matlab
Before MidTerm After MidTerm Eamonn Keogh
2
Conditional Branching
Get the Users Age Check if age less than 18 Yes No Display Warning Message Continue…
3
Conditional Branching
Get the Users Age Check if age less than 18 No Yes Display Confirmation Message Display Warning Message Continue…
4
Relational Expressions
EDU>> 2 < -7 ans = EDU>> 2 <= abs(-7) 1 EDU>> 2 == max(2,0) EDU>> ans = 2 < 7 EDU>> ans = 2 + 7 EDU>> ans = 2 < EDU>> ans = max(2,1) < 10 EDU>> ans = 2 == 19 EDU>> ans = 2 * 7 EDU>> ans = 2 ~= HisAge + 1 How can we recognize a relational expression? If there is one of the following <, <=, >, >=, ==, ~=, then it is an relational expression. Relational expressions evaluative to 1 or 0, nothing else is possible. Matlab uses ‘0’ to mean false Matlab uses ‘1’ to mean true
5
We do conditional Branching with the if statement
UsersAge = 12; if UsersAge < 18 disp('You cannot vote') end if some logical expression is true do these statements The code here is called the if block (of code). The if block can have as many lines of code as we like.
6
We do conditional Branching with the if statement
if UsersAge < 18 disp('You cannot vote') end (Minor note: You can write this on a single line, so long as you put commas after each statement) if UsersAge < 18, disp('You cannot vote') , end You should not do this, this is just to allow me to make terser slides
7
We can generalize the if statement to include the else clause
if some logical expression is true do these statements else do these statements instead end The if block The else block The if block and the else block can be different sizes
8
We can generalize the if statement to include the else clause
UsersAge = 12; if UsersAge < 18 disp('You cannot vote') else disp('You are allowed to vote') end
9
FishCount = GetNumberOfFish(); if FishCount < 10 disp(' Hard Luck')
else disp(' Good Catch') end In the above code, exactly one of ‘ Hard Luck’ or ‘ Good Catch’ will happen. There is no way that both could happen, and there is no way neither could happen. The if with else statement divides the world into two mutually exclusive worlds
10
function UserAge = GetUsersAgeBilingual()
Let us write a function to test our new skill. Our function gets a users age, prompting her in here choice of English or Spanish function UserAge = GetUsersAgeBilingual() LanguageChoice = input('Press 1 for English, Toca 2 para español : '); if LanguageChoice == 1 UserAge = input('Please Enter your age: '); else UserAge = input('Por favor ingrese su edad : '); end
11
EDU>> WifesAge = GetUsersAgeBilingual()
Press 1 for English, Toca 2 para español : 1 Please Enter your age: 23 WifesAge = 23 Press 1 for English, Toca 2 para español : 2 Por favor ingrese su edad : 19 19 EDU>>
12
function UserAge = GetUsersAgeBilingual()
LanguageChoice = input('Press 1 for English, Toca 2 para español : '); if LanguageChoice == 1 UserAge = input('Please Enter your age: '); disp('Thank you'); else UserAge = input('Por favor ingrese su edad : '); disp('Gracias') end
13
EDU>> MothersAge = GetUsersAgeBilingual()
Press 1 for English, Toca 2 para español : 1 Please Enter your age: 67 Thank you MothersAge = 67 Press 1 for English, Toca 2 para español : 2 Por favor ingrese su edad : 75 Gracias 75
14
Digression function UserAge=GetUsersAgeBilingual() %Bad
Likewise, we will use spaces in an elegant way…. function UserAge=GetUsersAgeBilingual() %Bad function UserAge = GetUsersAgeBilingual() %Good
15
Digression function UserAge = GetUsersAgeBilingual()
LanguageChoice = input('Press 1 for English, Toca 2 para español : '); if LanguageChoice == 1 UserAge = input('Please Enter your age: '); disp('Thank you'); else UserAge = input('Por favor ingrese su edad : '); disp('Gracias') end Digression These two functions are identical. In the one below I just removed the blank lines and left justified everything From now on, all you code should be pretty-print like the above. I may not always do this in my slides for space reasons. function UserAge = GetUsersAgeBilingual() LanguageChoice = input('Press 1 for English, Toca 2 para español : '); if LanguageChoice == 1 UserAge = input('Please Enter your age: '); disp('Thank you'); else UserAge = input('Por favor ingrese su edad : '); disp('Gracias') end
16
A classic use of a if/else statement is dividing a number line into two distinct possibilities.
Let us look at age… Age Child/Adult 10 Mile per Hour Speeding? 100 Blood Sugar Count Diabetic? 40 Net Worth Rich/Poor 10,000
17
function AdmissionPrice = GetDisneyAdmissionPrice()
AgeOfTicketBuyer = GetUsersAgeBilingual(); if AgeOfTicketBuyer <= 10 AdmissionPrice = 59; % They are a child else AdmissionPrice = 80; % They are a adult end Age Child/Adult 10
18
function AdmissionPrice = GetDisneyAdmissionPrice()
AgeOfTicketBuyer = GetUsersAgeBilingual(); if AgeOfTicketBuyer <= 10 AdmissionPrice = 59; % They are a child else AdmissionPrice = 80; % They are a adult end Age Child/Adult 10
19
EDU>> ToBill = GetDisneyAdmissionPrice
Press 1 for English, Toca 2 para español : 1 Please Enter your age: 5 Thank you ToBill = 59
20
Digression function AdmissionPrice = GetDisneyAdmissionPrice()
This code is a little shorter, but logically identical. Either is fine, perhaps the longer one might be easer to read and understand function AdmissionPrice = GetDisneyAdmissionPrice() if GetUsersAgeBilingual() <= 10 AdmissionPrice = 59; % They are a child else AdmissionPrice = 80; % They are a adult end Age Child/Adult 10
21
We can use the if/else statement to divde the world into two cases:
Adult/Child, English/Spanish, Jew/Gentile Suppose we have three cases? Toddler/Child/Adult, English/Spanish/French, Jew/Catholic/Atheist Matlab allows this with the elseif statement Toddler Child Adult 10 Age
22
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
23
function AdmissionPrice = NewGetDisneyAdmissionPrice()
AgeOfTicketBuyer = GetUsersAgeBilingual(); if AgeOfTicketBuyer <= 3 AdmissionPrice = 45; % They are a Toddler elseif AgeOfTicketBuyer <= 10 AdmissionPrice = 59; % They are a Child else AdmissionPrice = 80; % They are a Adult end This code “goes” left to right Toddler Child Adult 10 Age
24
function AdmissionPrice = NewGetDisneyAdmissionPrice()
AgeOfTicketBuyer = GetUsersAgeBilingual(); if AgeOfTicketBuyer >= 11 AdmissionPrice = 80; % They are a Adult elseif AgeOfTicketBuyer >= 4 AdmissionPrice = 59; % They are a Child else AdmissionPrice = 45; % They are a Toddler end This code “goes” right to left Toddler Child Adult 10 Age
25
function UserAge = GetUsersAgeMultilingual()
LanguageChoice = input('1 for English, 2 para español, 3 pour le français : '); if LanguageChoice == 1 UserAge = input('Please Enter your age: '); elseif LanguageChoice == 2 UserAge = input('Por favor ingrese su edad : '); else UserAge = input('Entrez votre âge? : '); end EDU>> GetUsersAgeMultilingual() 1 for English, 2 para español, 3 pour le français : 1 Please Enter your age: 34 ans = 34 1 for English, 2 para español, 3 pour le français : 2 Por favor ingrese su edad : 56 56 1 for English, 2 para español, 3 pour le français : 3 Entrez votre âge? : 78 78
26
HomeWork III (due Wed 1st at 8:10am)
You can do this homework with paper and pen, however you should use four different color pens to highlight the code, as in the sample below. Print VERY carefully, and remember all syntax, every ; ‘ ‘ ( ) etc function CigsPerDay = GetUse***** % Eamonn Keogh % This program asks the user for*** CigsPerDay = input(' Enter how m.. if CigsPerDay ********************* disp(‘You..*********************** elseif CigsP************************* disp(‘****************************** else disp('****************************** end Write a function that asks the user how many cigarettes a day they smoke, and returns that number. The function should also display one of three messages for a nonsmoker, a light smoker, and a heavy smoker (you can define the last two)
27
HomeWork III (due Wed 1st at 8:10am)
You can do this homework with paper and pen, however you should use four different color pens to highlight the code. Write a function in which you pass in one augment, a code for a language. If the function is called with a ‘1’, display “Felicitaciones” If the function is called with a ‘2’, display “Parabéns!” If the function is called with a ‘3’, display “Congratulazioni” The function will always return NaN. EDU>> DispCongrats(2) Parabéns!” ans = NaN function DummayVar = DispCo***** % Eamonn Keogh % This program ********** DummayVar =NaN: if ***************** disp(‘Felicita********************* elseif *******************
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.