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

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

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 8 - Programming II Today: –Control flow options beyond if/else –Loop options beyond for –Nesting Debugging tools Textbook chapter 7, pages 178-192 (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

Could use “nested” if/else commands

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”

“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

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

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

Similar presentations