CSC 115 Matlab Special Session Dr Zhen Jiang
CSC 115: Matlab Special Session Dr. Zhen Jiang Computer Science Department West Chester University
�Control Flow ◦ If/else ◦ For/while
Selection (Decision) �Rolling a dice. ◦ Sample execution (click on this link to try) ◦ Each button in the above sample has
Yes No Win/Lost? Double the money Bankrupt Restart
�If else statement if (condition) action 1 (statements 1) else action 2 (statements 2) end action 3 (statement 3)
Yes No Condition Action 1 Action 2 Action 3
�Condition ◦ Simple condition �Format <Value> <relational operators> <Value> �Number value relational operators, Table 10 -1, page 189 ==, ~=, <, >, <=, >= !!! Number one error: “(a=2)” instead of “(a==2)”
�Condition ◦ Complex condition �&&, ||, ~ (Table 10 -2, page 192) �Truth table C 1 C 2 C 1 && C 2 T T F F F T F F C 1 || C 2 ~ C 1 T F T �Precedence order, table 10 -3, page 193
Development Process � Identify two exclusive options � Implement each handling in different action parts � Identify the situation (values) for option selection � Make a condition so that all the situation value for option part 1 will lead to this condition true. � Verify all the situation value for option part 2 will lead to this condition false, otherwise, revise the above condition!
�If statement, �Multiple selection ◦ Nested if ◦ If else if, page 208 ◦ Switch, page 210 �Example: letter grade
�Comments: ◦ Nested if for multiple selection problem If case 1 Else if case 2 else … end %of case 2 if end %of case 1 if
Loop �Price is right. ◦ Sample execution (click on this link to try) ◦ Each button in the above sample REPEAT …?
n For loop n n n Format, page 200. Logic. Sample, page 201.
n While loop n n Format & Logic, page 206 Sample.
n Development process
�Exercise 1+2+4+8+. . . 1+2+3+4+. . . +99
�List client number, client name, street, for all clients that live in zip code “ 80336” �Example_column Client Number AC 34 BH 72 BL 12 EA 45 FD 89 FH 22 MH 56 PR 11 TC 37 WL 56 Client Name Alys Clinic Berls Hospital Benton Labs ENT Assoc. Ferb Dentistry Family Health Maun Hospital Peel Radiology Tarleton Clinic West Labs CO CO 80330 80349 Amount Recruiter Current Due Paid Number $0. 00 $17, 500. 00 21 $29, 200. 00 $0. 00 24 12 Mountain Denton CO 867 Ridge Fort Stewart CO 34 Crestview Berridge CO 80412 80336 80330 $16, 500. 00 $38, 225. 00 24 $12, 750. 00 $15, 000. 00 27 $21, 000. 00 $12, 500. 00 21 123 Second Tarleton CO 80409 $0. 00 76 Dixon Mason CO 80356 $0. 00 $43, 025. 00 24 151 Valleyview 451 Hull Fort Stewart CO 80336 $31, 750. 00 Tarleton CO 80409 $18, 750. 00 $31, 500. 00 27 785 Main Berls CO 80349 $14, 000. 00 Street City 134 Central Berridge 415 Main Berls State Postal Code $0. 00 24 $0. 00 21 $0. 00 24
Display all words (or all numbers)
�Handles both numbers and words �List client number, client name, street and zip code (Number!!), for all clients that live in zip code “ 80336” �Example_column
�You have to convert number to string (word) format (in order to display together in one line) – num 2 str(zip. Num) �One zip is converted each time, forcing the program handle the display line by line.
�What do you see, for the program in that box, to repeat many times? ◦ Not display ◦ Insert each line to result, ready for display �Verify the repetition body: ◦ What the computer do in the first round? ◦ What the computer do in the second round? ◦ What the computer do in the third round? ◦ result = [result; …, num 2 str(zip(line))]
�Repetition body (continue) ◦ Is it optional? ◦ What the value to determine the selected case? ◦ Can you make the condition? �If(zip == 80336) ◦ Values verification �True �False
�For loop ◦ Do we know how many times to repeat? ◦ 2 11 is equal to 1 10 �Initialization ◦ How we insert the first line when we do: result = [result; …, num 2 str(zip(line))] ◦ result = [ ] �Final touch! ◦ For i=1: 10 =>zip(1), zip(2), zip(3)… ◦ num 2 str(zip(i))
�Begin with, end with and found
�You have to check the match of each string (word), not the whole array of string – regexp (or strfind) �Using regexp �Not giving you the line number, giving you some results strange. �Need to check if the result is empty. Otherwise, it is selected record. �One check each time, forcing the program handle the display line by line.
�What do you see, for the program in that box, to repeat many times? ◦ Not display ◦ Insert line number to result, ready for further use �Verify the repetition body: ◦ What the computer do in the first round? ◦ What the computer do in the second round? ◦ What the computer do in the third round? ◦ result = [result; linenumber]
�Repetition body (continue) ◦ Is it optional? ◦ What the value to determine the selected case? ◦ Can you make the condition? �If(regexp/strfind matched) �Regexp(string, ‘*6’, ‘end’) �Strfind(string, ‘H 7’) �Cannot support “strfind (string, ‘*H 7’)” ◦ Values verification �True �False
�For loop ◦ Do we know how many times to repeat? ◦ 2 11 is equal to 1 10 �Initialization ◦ How we insert the first line when we do: result = [result; linenumber] ◦ result = [ ] �Final touch! ◦ For i=1: 10 =>1, 2, 3, … ◦i
�Handles top value �For recruiter “ 24”, list the top 3 clients (client number, client name, amount paid) who have the highest amount paid ◦ Sort in the descending order by amount paid ◦ Find the original index/position that whose record meeting the simple criteria of “recruiter 24”. ◦ Prepare the display data ◦ If the criteria is not simple, find the selected record with if check during the data preparation process. �Example_sort
�You have to convert number to string (word) format (in order to display together in one line) – num 2 str(amount) �One is converted each time, forcing the program handle the display line by line.
�What do you see, for the program in that box, to repeat many times? ◦ Not display ◦ Insert each line to result, ready for display �Verify the repetition body: ◦ What the computer do in the first round? ◦ What the computer do in the second round? ◦ What the computer do in the third round? ◦ result = [result; …, num 2 str(amount(r))]
�Repetition body (continue) ◦ Is it optional? ◦ If yes, what the value to determine the selected case, and the corresponding condition? ◦ This is for a complex criteria to select a line record.
�For loop ◦ Do we know how many times to repeat? ◦ Top 3! �Initialization ◦ How we insert the first line when we do: result = [result; …, num 2 str(amount(r)] ◦ result = [ ] �Final touch! ◦ For i=1: 3 =>amount(r(1)), amount(r(2)), … ◦ num 2 str(amount(r(i))
- Slides: 44