Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > = <= == ~= Logical & | If operations have equal precedence the expression is executed from left to right What is the value of -2 > 5 > 1 ?
Week 9 - Programming II Today: –Control flow options beyond if/else –Loop options beyond for –Nesting Debugging tools Textbook chapter 7, pages , (sections 7.2.3, 7.3, 7.4.2, 7.5, 7.6 )
Extensions of if/else As introduced, if/else allows for two choices: if expression {commands if expression is true } else {commands if false } end
What if there are more than 2 situations? 3 situations: for a variable x, display: x = 0 x > 0 x < 0 4 situations: convert a compass angle to a direction: 0º east 90º north 180º west 270º south
Could use “nested” if/else commands
or
The “elseif” command if expression1 {commands if expression1 is true } elseif expression2 {commands if expression2 is true } else {commands if both are false } end
Examples: Note – many elseifs are allowed, but only 1 “else”
% HI-LO numb = round ( 100 * rand (1) ); for count = 1:5 guess = input('guess my number '); if guess = = numb disp( 'You got it !!!' ) break elseif guess > numb disp('you are too high') else disp('you are too low') end if count = = 5 disp('Sorry, you lose! ') end simpler comparison
“Switch/Case” Use the value of a single variable or expression to determine what commands to execute: switch expression case value1 {command set 1} case value2 {command set 2} case value3 {command set 3} otherwise {command set 4} end
Example – convert compass angle to direction: note combining of possible values in braces note use of otherwise
Other control flow options: continue – jumps to next loop iteration: e.g. for k = 1:25000 if x(k) > 0 continue end { more commands } end return – terminates current function or script skip ahead
Longer Running Loops for loops repeat a fixed number of times: for variable = {array of length n} {commands} end and break can be used to stop earlier. Question: How about repeating “until done”?
Answer: Matlab’s “while” loop: while expression {commands to be repeated while expression is true} end
Example 1 – compounded interest until the amount doubles: value = 1000; for year = 1:1000 value = value * 1.08; fprintf('%2d years: %6.2f \n',year,value) if value > 2000 break end for loop terminated with break
Expected output:
while version value = 1000; year = 0; while value < 2000 value = value * 1.08; year = year + 1; fprintf('%2d years: %6.2f \n',year,value) end note the counter variable year
Example 3 – keeping time: time = input('how long to wait? '); while time > 0 disp([ num2str(time),' seconds left']) pause(1) time = time – 1; end disp('done') unnecessary relational op yet another counter variable
Example 4 – Collecting and storing data until a zero is entered: x = [ ]; new = 1; while new ~= 0 new = input('enter value '); x = [ x, new ]; end x = x(1:end–1) empty array to drop the zero initialize
Example 5 – Getting valid keyboard input: E.g. forcing the input to be between 0 and 10: x = -3; while ( x 10 ) x = input( 'type a value ' ); end
or: x = input('enter value '); while (x 10) disp('invalid choice'); x = input('enter value '); end disp('finally!');
Put These Together – Hi-Lo: numb = round (10*rand(1)); done = 0; while ~done guess = input('guess'); if guess = = numb disp( 'You got it !!!' ); done = 1; elseif guess > numb disp('too high') else disp('too low') end initialization single guess loop control
Loops within Loops – Nesting while expression1 {commands} while expression2 {commands} end {commands} end for index1 = array1 {commands} for index2 = array2 {commands} end {commands} end can be more than 2 levels deep
Example – computing a table of z = x 2 +y 2 for x and y equal to the integers 1, 2,…6: for x = 1:6 for y = 1:6 z(x,y) = x^2+y^2; end z
Debugging Tools so far, Run
Debugging ≡ finding and correcting errors (bugs) in programs Useful debugging tools: –Ability to stop a program in the middle of its execution (at a breakpoint) –Ability to examine variable values at that point –Ability to modify variable values at that point
controls for creating and removing breakpoints
indicator of breakpoint location (can have multiple breakpoints)
What shows up at the breakpoint Command window:Editor window: location indicator different prompt
Can single step (F10) or continue (F5) to the next breakpoint (or end)