Download presentation
Presentation is loading. Please wait.
Published bySamuel Whitehead Modified over 9 years ago
1
CSC115: Matlab Special Session Dr. Zhen Jiang Computer Science Department West Chester University
2
Control Flow ◦ If/else ◦ For/while
3
Rolling a dice. ◦ Sample execution (click on this link to try)this link ◦ Each button in the above sample has Selection (Decision)
4
Win/Lost? Yes Double the money Bankrupt No Restart
5
If else statement if (condition) action 1 (statements 1) else action 2 (statements 2) end action 3 (statement 3)
6
Condition Yes Action 1Action 2 No Action 3
7
Condition ◦ Simple condition Format Number value relational operators, Table 10-1, page 189 ==, ~=,, = !!! Number one error: “(a=2)” instead of “(a==2)”
8
Condition ◦ Complex condition &&, ||, ~ (Table 10-2, page 192) Truth table Precedence order, table10-3, page 193 C1TTFF C2TFTF C1 && C2TFFF C1 || C2TTTF ~ C1FFTT
9
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! Development Process
10
If statement, Multiple selection ◦ Nested if ◦ If else if, page 208 ◦ Switch, page 210 Example: letter grade
11
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
13
Price is right. ◦ Sample execution (click on this link to try)this link ◦ Each button in the above sample REPEAT …? Loop
14
For loop Format, page 200. Logic. Sample, page 201.
15
While loop Format & Logic, page 206 Sample.
17
Development process
20
Exercise 1+2+4+8+... 1+2+3+4+...+99
21
Client Number Client Name StreetCityState Postal Code Amount Paid Current Due Recruiter Number AC34Alys Clinic134 CentralBerridgeCO80330$0.00$17,500.0021 BH72Berls Hospital 415 MainBerlsCO80349$29,200.00$0.0024 BL12Benton Labs12 MountainDentonCO80412$16,500.00$38,225.0024 EA45ENT Assoc.867 RidgeFort StewartCO80336$12,750.00$15,000.0027 FD89Ferb Dentistry 34 Crestview BerridgeCO80330$21,000.00$12,500.0021 FH22Family Health 123 SecondTarletonCO80409$0.00 24 MH56Maun Hospital 76 DixonMasonCO80356$0.00$43,025.0024 PR11Peel Radiology 151 Valleyview Fort StewartCO80336$31,750.00$0.0021 TC37Tarleton Clinic 451 HullTarletonCO80409$18,750.00$31,500.0027 WL56West Labs785 MainBerlsCO80349$14,000.00$0.0024 List client number, client name, street, for all clients that live in zip code “80336” Example_column
23
Display all words (or all numbers)
24
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
25
?
26
You have to convert number to string (word) format (in order to display together in one line) – num2str(zipNum) One zip is converted each time, forcing the program handle the display line by line.
27
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;…, num2str(zip(line))]
28
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
29
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;…, num2str(zip(line))] ◦ result = [ ] Final touch! ◦ For i=1:10 =>zip(1), zip(2), zip(3)… ◦ num2str(zip(i))
31
Begin with, end with and found
32
?
33
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.
34
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]
35
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, ‘H7’) Cannot support “strfind (string, ‘*H7’)” ◦ Values verification True False
36
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◦i
38
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
39
?
40
You have to convert number to string (word) format (in order to display together in one line) – num2str(amount) One is converted each time, forcing the program handle the display line by line.
41
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; …, num2str(amount(r))]
42
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.
43
For loop ◦ Do we know how many times to repeat? ◦ Top 3! Initialization ◦ How we insert the first line when we do: result = [result; …, num2str(amount(r)] ◦ result = [ ] Final touch! ◦ For i=1:3 =>amount(r(1)), amount(r(2)), … ◦ num2str(amount(r(i))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.