440 likes | 536 Views
CSC115: 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. No. Yes. Win/Lost?.
E N D
CSC115: 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
No Yes 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)
No Yes 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 • Precedence order, table10-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 …?
For loop • Format, page 200. • Logic. • Sample, page 201.
While loop • Format & Logic, page 206 • Sample.
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
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) – num2str(zipNum) • 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;…, num2str(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? • 211 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))
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, ‘H7’) • Cannot support “strfind (string, ‘*H7’)” • Values verification • True • False
For loop • Do we know how many times to repeat? • 211 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) – num2str(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; …, num2str(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;…, num2str(amount(r)] • result = [ ] • Final touch! • For i=1:3 =>amount(r(1)), amount(r(2)), … • num2str(amount(r(i))