Presentation is loading. Please wait.

Presentation is loading. Please wait.

Eamonn Keogh eamonn@cs.ucr.edu CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu.

Similar presentations


Presentation on theme: "Eamonn Keogh eamonn@cs.ucr.edu CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu."— Presentation transcript:

1 Eamonn Keogh eamonn@cs.ucr.edu
CS005 Introduction to Programming: Matlab Eamonn Keogh

2

3 Review: Incrementing a Variable
Adding one to a variable is called incrementing It is an important trick we use a lot with loops We do it by… >> HisAge = 25; % Give some value >> HisAge = HisAge + 1; % Increment HisAge Before incrementing HisAge 3 inf 25 18 4 After incrementing HisAge 3 inf 26 18 4

4 Review: Incrementing a Variable
Subtracting one from a variable is called decrementing We do it by… >> HisAge = 25; % Give some value >> HisAge = HisAge - 1; % Increment HisAge We can increment by more than one, for example, increment by five >> HisAge = HisAge + 5; % Increment HisAge But if we say just increment, we mean one

5 Review: We have seen how to use the for loop
We can repeat a code block multiple times with the for statement for index = values do these statements end Review: We have seen how to use the for loop

6 Review: We have seen how to use the for loop
Start with i at this value Stop when i hits this value for i = 1 : 10 do these statements end Make i one larger when you get here Review: We have seen how to use the for loop

7 We can repeat a code block multiple times with the while statement
while expression do these statements end What is the difference between a for loop and a while loop? Informally If we know just before we get to the loop, how often it will run, we want a for loop If we don’t know just before we get to the loop, how often it will run, we want a while loop

8 % This is a script called DemoWhileEamonn i = 1; % Initialize i
while i <= 10 disp(i); % Display i i = i + 1; % Increment i end EDU>> DemoWhileEamonn 1 2 3 4 5 6 7 8 9 10

9 i = 1; % Initialize i while i <= 10 disp(i);
disp(' times five is '); disp(i * 5); disp(' '); i = i + 1; % Increment i end EDU>> DemoWhileEamonn 1 times five is 5 2 10 Anything we did with a for loop, we could do with a while loop. However, we are responsible for incrementing the index variable

10 % I forgot it increment the index variable!!! end
i = 1; % Initialize i while i <= 10 disp(i); disp(' times five is '); disp(i * 5); disp(' '); % I forgot it increment the index variable!!! end EDU>> DemoWhileEamonn 1 times five is 5 (a few hundred years from now…) e+017 e+018 We are responsible for incrementing the index variable. Failing to do so, will produce an infinite loop

11 EDU>> TaxPayersAge = GetUsersAge(); Enter your age : 34
EDU>> TaxOwed = 1000/ TaxPayersAge TaxOwed = Let us imagine that a tax law requires every adult to pay a charge equal to your $1000 divided by your age. The code above shows what the code we write might look like… We just need to write the GetUsersAge function…

12 function UsersAge = GetUsersAge()
UsersAge = input('Enter your age : '); end This function works, but it is brittle. It allows the user to enter values that are impossible, and these strange values can trickle up the rest of the code and produce strange results… EDU>> TaxPayersAge = GetUsersAge(); Enter your age : 0 EDU>> TaxOwed = 1000/ TaxPayersAge TaxOwed = Inf EDU>> TaxPayersAge = GetUsersAge(); Enter your age : EDU>> TaxOwed = 1000/ TaxPayersAge TaxOwed = -1000

13 The solution is to write a robust function that will get a reasonable value.
while loops are perfect for this…. The basic idea is to give the user an initial chance to enter his/her age, and while the value is unacceptable, ask again….

14 function UsersAge = RoubustGetUsersAge()
UsersAge = input('Enter your age : '); while UsersAge < 18 UsersAge = input('You made a mistake, Enter your age : '); end EDU>> TaxPayersAge = RoubustGetUsersAge() Enter your age : 55 TaxPayersAge = 55 EDU>> TaxPayersAge = RoubustGetUsersAge() Enter your age : 7 You made a mistake, Enter your age : 9 You made a mistake, Enter your age : 22 TaxPayersAge = 22

15 function UsersAge = RoubustGetUsersAge() % Do NOT use
UsersAge = input('Enter your age : '); while UsersAge < 18 Age = input('You made a mistake, Enter your age : '); end This function has a bug. We must always make sure there is a way out of the while loop EDU>> TaxPayersAge = RoubustGetUsersAge() Enter your age : 2 You made a mistake, Enter your age : 21 You made a mistake, Enter your age : 22 You made a mistake, Enter your age : 222 You made a mistake, Enter your age : 21112 You made a mistake, Enter your age : stop!!

16 Let us write a script that converts numbers from inches to millimeters, while the user wants to continue.. We need a way for the user to signal that he/she is finished, let us use a negative number to signal this… EDU>> inches2mm I will convert inches to mm for you. Enter number to convert or negative number to quit : 2 2 inches is mm Enter number to convert or negative number to quit : -1 Thank you for using my program Sidenote Matlab functions/scripts that convert one thing to another are usually called something2somethingelse str2num hex2num cart2compass

17 Enter number to convert : 2 2 inches is 50.8000 mm
NumToConvert = input('Enter number to convert :'); disp(NumToConvert); disp(' inches is '); disp(NumToConvert * 25.4) disp(' mm') EDU>> inches2mm Enter number to convert : 2 2 inches is mm We will slowly build up to our full script

18 The last time through the loop it converts the exit value
NumToConvert = 0; % Initialize to make sure loop happens at least once disp('I will convert inches to mm for you.') while NumToConvert >= 0 NumToConvert = input('Enter number to convert or negative number to quit : '); disp(NumToConvert); disp(' inches is '); disp(NumToConvert * 25.4) disp(' mm') end disp('Thank you for using my program') EDU>> inches2mm I will convert inches to mm for you. Enter number to convert or negative number to quit : 2 2 inches is mm Enter number to convert or negative number to quit : -1 -1 Thank you for using my program This function has a bug. The last time through the loop it converts the exit value

19 NumToConvert = 0; % Initialize to make sure loop happens at least once
disp('I will convert inches to mm for you.') while NumToConvert >= 0 NumToConvert = input('Enter number to convert or negative number to quit : '); if NumToConvert >= 0 % User wants to convert this value disp(NumToConvert); disp(' inches is '); disp(NumToConvert * 25.4) disp(' mm') end disp('Thank you for using my program') This fixes it… EDU>> inches2mm I will convert inches to mm for you. Enter number to convert or negative number to quit : 2 2 inches is mm Enter number to convert or negative number to quit : -1 Thank you for using my program

20 This code is slightly more elegant and readable
NumToConvert = 0; % Intialize to make sure loop happens at least once disp('I will convert inches to mm for you.') while NumToConvert >= 0 NumToConvert = input('Enter number to convert or negative number to quit : '); if NumToConvert >= % User wants to convert this value disp(NumToConvert); disp(' inches is '); disp(NumToConvert * 25.4) disp(' mm') else % User wants to quit disp('Thank you for using my program') end EDU>> inches2mm I will convert inches to mm for you. Enter number to convert or negative number to quit : 2 2 inches is mm Enter number to convert or negative number to quit : -1 Thank you for using my program This code is slightly more elegant and readable

21 GetAgesOfTeamPlayers
Let us write a function that gets the ages of players in a team. We have done this before, but we assumed we knew how many players there are ahead of time. However, this time we assume we don’t know. So we will keep adding ages while there are more players GetAgesOfTeamPlayers [ ]

22 We will slowly build up to our full function.
function ArrayOfTeamAges = GetAgesOfTeamPlayers() temp = input('How old? Type -1 to finish : '); % Prompt the user if temp > % He wants to add a player disp('this means process age ') else disp('this means quit') % User has signaled to quit end EDU>> GetAgesOfTeamPlayers() How old? Type -1 to finish : 4 this means process age How old? Type -1 to finish : -1 this is quit We will slowly build up to our full function.

23 We will slowly build up to our full function.
function ArrayOfTeamAges = GetAgesOfTeamPlayers() temp = input('How old? Type -1 to finish : '); % Prompt the user if temp > % He wants to add a player disp('this means process age ') ArrayOfTeamAges(1) = temp; % Add the age to array else disp('this means quit') % User has signaled to quit end EDU>> GetAgesOfTeamPlayers() How old? Type -1 to finish : 56 this means process age ans = 56 We will slowly build up to our full function.

24 function ArrayOfTeamAges = GetAgesOfTeamPlayers()
PlayerID = 1; % Start with player 1 while PlayerID > % Check if there is another player to add temp = input('How old? Type -1 to finish : '); % Prompt the user if temp > % He wants to add a player ArrayOfTeamAges(PlayerID) = temp; % Add the age to array PlayerID = PlayerID + 1; % Increment to next player else PlayerID = ; % User has signaled to quit end EDU>> GetAgesOfTeamPlayers() How old? Type -1 to finish : 34 How old? Type -1 to finish : 51 How old? Type -1 to finish : -1 ans = EDU>>


Download ppt "Eamonn Keogh eamonn@cs.ucr.edu CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu."

Similar presentations


Ads by Google