270 likes | 402 Vues
COMP 116: Introduction to Scientific Programming . Lecture 28 : Midterm #2 Review. Announcements. Midterm in SN014. Topics. Conditional Logic Writing Simple Functions Variables, workspaces, scope Loops Strings. Conditional Logic. Conditional Programming
E N D
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review
Announcements • Midterm in SN014
Topics • Conditional Logic • Writing Simple Functions • Variables, workspaces, scope • Loops • Strings
Conditional Logic • Conditional Programming • When you want to run some code only if some test (“condition”) is satisfied • Making choices between 2 or more options • if-else-end
Relational OperatorsTests relationship between two objects or arrays • What is the data type of the result of these operators?
Relational OperatorsTests relationship between two objects or arrays • Result is always a logical data type (or an array of logical data types)
Logical OperatorsBoolean operators • Operates on logical data types, also returns a logical result.
Other Logical Objects • Logical Constants: true, false • Logical Functions: and(), or(), xor(), not() • Predicate Logic: any(), all() • Conversion Function: logical() • Test functions (is*functions) • Examples: isvarname(), iskeyword() • String Comparison functions: • strcmp(), strcmpi(), strncmp(), strncmpi()
Two options for logical indexing • Use a logical vector • x = rand(1, 100); • x(x > 0.5); % returns only those elements in x which satisfy test • Use the find() function • y = find( x > 0. 5 ); • x( y ) % returns elements in x whose indices are in y
Conditional ExecutionMultiple chained tests if <Test1> commands1; % T1 true elseif <Test2> commands2; % T1 false T2 true elseif <Test3> commands3; % T1,T2 false T3 true else commands4; % all false end
Text Output • disp(msg)- displays an array or text string • disp(‘Derp'); • error(msg) - displays an error message and aborts current function or script • error('Value Out of Range');
Writing simple functions function [o1, o2] = funcName( i1, i2 ) % function comments … % body (implementation) end • Can have multiple inputs and multiple outputs • function [] = funcName() • function o1 = funcName() • function o1 = funcName( i1 ) • function o1 = funcName( i1, i2 ) • function [o1, o2] = funcName( i1, i2, i3)
Scope • Functions run in their own ‘workspaces’ MATLAB sq.m foo =4 bar =16 x2 =5 x =4 x2 =16
Loops: for loop statementthe counted loop solution for <varindex> = <start>:<stop> <Body: do some work> end for <idx> = <start>:<step>:<stop> <Body: do some work> end
Loops: while loop statementthe conditional loop solution while <test> <Body: do some work> <Update:make progress towards exiting loop> end • While loops are great if we don’t know how many times we need to loop, but if we can write a test for when we’re done • For this to work properly, the test needs to evaluate to a logical value • The while loop will run as long as test evaluates to true • The while loop does not have a built-in counter like the for-loop (if you want to count something, you need to implement the counter yourself)
Loops: Common pitfalls • While-loops: • Counters not initialized • While-loop never terminates or gets never executed • Counter does not count all the way to the desired value: e.g., x<5 instead of x<=5
Common Idioms: Check if value is contained within vector Practice: Rewrite this using a while loop
Common Idioms: Check if value is contained within vector Note the two conditions, understand why this works
Common Idioms: Looping over a matrix • Use a nested for loop: • function ret = findMaxElement( A ) • sz = size(A); • ret = A(1,1); • for i=1:sz(1) • for j=1:sz(2) • if ( A(i,j)>ret ) • ret = A(i,j); • end • end • end
Strings as a vector of chars • Can be manipulated like any other vector s1 = 'The quick brown fox ' s2 = 'jumped over the lazy dog' s = [s1 s2] % concatenate strings s(5) % ans = q s(17:19) % ans = fox jIdx = find( s == 'j' ) jStr = s(jIdx:jIdx+3) % ans = jump
String Comparison • Avoid normal comparison operators! • s1 == s2, s1 < s3, s1 >= s3 • Operators work element by element (on characters) • Thus, strings (i.e., the vector of chars) must be same length • Use string comparison functions instead • strcmp(), string comparison • strcmpi, string comparison while ignoring case • strncmp, strncmpi: • Similar, but compares first n characters only
String Searching • strfind • Search for a string inside another string • returns indices to start of each instance strVal = [‘with great power comes great responsibility.’]; strfind( strVal, ‘great’) % ans = [6 24]
String Replacement • strrep strVal = [‘with great power comes great responsibility.’]; strrep( strVal, ‘great’, ‘no’)
Summary • Practice, practice, practice • Work through the examples done in class