Presentation is loading. Please wait.

Presentation is loading. Please wait.

Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > = <= == ~= Logical & | If operations have equal precedence the expression.

Similar presentations


Presentation on theme: "Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > = <= == ~= Logical & | If operations have equal precedence the expression."— Presentation transcript:

1 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 ?

2 Week 9 - Programming II Today: –Control flow options beyond if/else –Loop options beyond for –Nesting Debugging tools Textbook chapter 7, pages 176-180, 185-199 (sections 7.2.3, 7.3, 7.4.2, 7.5, 7.6 )

3 Extensions of if/else As introduced, if/else allows for two choices: if expression {commands if expression is true } else {commands if false } end

4 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

5 Could use “nested” if/else commands

6 or

7 The “elseif” command if expression1 {commands if expression1 is true } elseif expression2 {commands if expression2 is true } else {commands if both are false } end

8 Examples: Note – many elseifs are allowed, but only 1 “else”

9 % 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

10 “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

11 Example – convert compass angle to direction: note combining of possible values in braces note use of otherwise

12 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

13 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”?

14 Answer: Matlab’s “while” loop: while expression {commands to be repeated while expression is true} end

15 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

16 Expected output:

17 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

18 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

19 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

20 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

21 or: x = input('enter value '); while (x 10) disp('invalid choice'); x = input('enter value '); end disp('finally!');

22 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

23 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

24 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

25 Debugging Tools so far, Run

26 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

27 controls for creating and removing breakpoints

28 indicator of breakpoint location (can have multiple breakpoints)

29 What shows up at the breakpoint Command window:Editor window: location indicator different prompt

30 Can single step (F10) or continue (F5) to the next breakpoint (or end)


Download ppt "Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > = <= == ~= Logical & | If operations have equal precedence the expression."

Similar presentations


Ads by Google