conditionals control flow announcements for this lecture
play

Conditionals & Control Flow Announcements For This Lecture - PowerPoint PPT Presentation

Lecture 8 Conditionals & Control Flow Announcements For This Lecture Readings Assignment 1 Sections 5.1-5.7 today Due TONIGHT Chapter 4 for Tuesday Due before midnight Submit something Assignment 2 Can


  1. Lecture 8 Conditionals & Control Flow

  2. Announcements For This Lecture Readings Assignment 1 • Sections 5.1-5.7 today • Due TONIGHT • Chapter 4 for Tuesday § Due before midnight § Submit something… Assignment 2 § Can resubmit to Sep. 28 • Grades posted Saturday • Posted Today § Written assignment • Complete the Survey § Do while revising A1 § Must answer individually 9/17/15 Conditionals & Control Flow 2

  3. Testing last_name_first(n) # test procedure Call function def test_last_name_first(): on test input """Test procedure for last_name_first(n)""" result = name.last_name_first('Walker White') Compare to cornelltest.assert_equals('White, Walker', result) expected output result = name.last_name_first('Walker White') cornelltest.assert_equals('White, Walker', result) # Application code Test code is properly if __name__ == '__main__': formatted as script test_last_name_first() print 'Module name is working correctly' 9/17/15 Conditionals & Control Flow 3

  4. Types of Testing Black Box Testing White Box Testing • Function is “opaque” • Function is “transparent” § Test looks at what it does § Tests/debugging takes place inside of function § Fruitful : what it returns § Focuses on where error is § Procedure : what changes • Example : Use of print • Example : Unit tests • Problems : • Problems : § Much harder to do § Are the tests everything? § Must remove when done § What caused the error? 9/17/15 Conditionals & Control Flow 4

  5. Finding the Error • Unit tests cannot find the source of an error • Idea: “Visualize” the program with print statements def last_name_first(n): """Returns: copy of <n> in form <last>, <first>""" end_first = n.find(' ') Print variable after print end_first each assignment first = n[:end_first] print 'first is '+str(first) Optional : Annotate last = n[end_first+1:] value to make it easier to identify print 'last is '+str(last) return last+', '+first 9/17/15 Conditionals & Control Flow 5

  6. Structure vs. Flow Program Structure Program Flow • Way statements are presented • Order statements are executed § Order statements are listed § Not the same as structure § Inside/outside of a function § Some statements duplicated § Will see other ways… § Some statements are skipped • Indicate possibilities over • Indicates what really happens multiple executions in a single execution Have already seen this difference with functions 9/17/15 Conditionals & Control Flow 6

  7. Structure vs. Flow: Example Program Structure Program Flow def foo(): >>> python foo.py Statement print 'Hello' 'Hello' listed once Statement 'Hello' executed 3x # Script Code 'Hello' if __name__ == 'main': foo() Bugs can occur when we foo() get a flow other than one foo() that we where expecting 9/17/15 Conditionals & Control Flow 7

  8. Conditionals: If-Statements Format Example if < boolean-expression >: # Put x in z if it is positive < statement > if x > 0: … z = x < statement > Execution : if <b oolean-expression > is true, then execute all of the statements indented directly underneath (until first non-indented statement) 9/17/15 Conditionals & Control Flow 8

  9. Conditionals: If-Else-Statements Format Example if < boolean-expression >: # Put max of x, y in z < statement > if x > y: … z = x else : else : < statement > z = y … Execution : if <b oolean-expression > is true, then execute statements indented under if; otherwise execute the statements indented under elsec 9/17/15 Conditionals & Control Flow 9

  10. Conditionals: “Control Flow” Statements b Branch Point: if b : Evaluate & Choose s1 # statement s1 s3 s3 Statement: Execute if b : b s1 Flow else : Program only s1 s2 takes one path s2 each execution s3 s3 9/17/15 Conditionals & Control Flow 10

  11. Program Flow and Call Frames def max(x,y): max(0,3) : """Returns: max of x, y""" # simple implementation max 1 1 if x > y: x 0 2 return x y 3 3 return y Frame sequence depends on flow 9/17/15 Conditionals & Control Flow 11

  12. Program Flow and Call Frames def max(x,y): max(0,3) : """Returns: max of x, y""" # simple implementation max 3 1 if x > y: x 0 2 return x y 3 3 return y Frame sequence Skips line 2 depends on flow 9/17/15 Conditionals & Control Flow 12

  13. Program Flow and Call Frames def max(x,y): max(0,3) : """Returns: max of x, y""" # simple implementation max 1 if x > y: x 0 RETURN 2 return x y 3 3 3 return y Frame sequence Skips line 2 depends on flow 9/17/15 Conditionals & Control Flow 13

  14. Program Flow vs. Local Variables def max(x,y): • temp is needed for swap """Returns: max of x, y""" § x = y loses value of x # swap x, y § “Scratch computation” # put the larger in y § Primary role of local vars 1 if x > y: • max(3,0) : 2 temp = x max 3 x = y 1 4 y = temp x y 3 0 5 return y 9/17/15 Conditionals & Control Flow 14

  15. Program Flow vs. Local Variables def max(x,y): • temp is needed for swap """Returns: max of x, y""" § x = y loses value of x # swap x, y § “Scratch computation” # put the larger in y § Primary role of local vars 1 if x > y: • max(3,0) : 2 temp = x max 3 x = y 2 4 y = temp x y 3 0 5 return y 9/17/15 Conditionals & Control Flow 15

  16. Program Flow vs. Local Variables def max(x,y): • temp is needed for swap """Returns: max of x, y""" § x = y loses value of x # swap x, y § “Scratch computation” # put the larger in y § Primary role of local vars 1 if x > y: • max(3,0) : 2 temp = x max 3 x = y 3 4 y = temp x y 3 0 temp 3 5 return y 9/17/15 Conditionals & Control Flow 16

  17. Program Flow vs. Local Variables def max(x,y): • temp is needed for swap """Returns: max of x, y""" § x = y loses value of x # swap x, y § “Scratch computation” # put the larger in y § Primary role of local vars 1 if x > y: • max(3,0) : 2 temp = x max 3 x = y 4 4 y = temp x y 0 0 temp 3 5 return y 9/17/15 Conditionals & Control Flow 17

  18. Program Flow vs. Local Variables def max(x,y): • temp is needed for swap """Returns: max of x, y""" § x = y loses value of x # swap x, y § “Scratch computation” # put the larger in y § Primary role of local vars 1 if x > y: • max(3,0) : 2 temp = x max 3 x = y 5 4 y = temp x y 0 3 temp 3 5 return y 9/17/15 Conditionals & Control Flow 18

  19. Program Flow vs. Local Variables def max(x,y): • temp is needed for swap """Returns: max of x, y""" § x = y loses value of x # swap x, y § “Scratch computation” # put the larger in y § Primary role of local vars 1 if x > y: • max(3,0) : 2 temp = x max 3 x = y 4 y = temp x y 0 3 temp 3 5 return y 3 RETURN 9/17/15 Conditionals & Control Flow 19

  20. Program Flow vs. Local Variables def max(x,y): • Value of max(3,0) ? """Returns: max of x, y""" A: 3 # swap x, y B: 0 # put the larger in y C: Error! if x > y: D: I do not know temp = x x = y y = temp return temp 9/17/15 Conditionals & Control Flow 20

  21. Program Flow vs. Local Variables def max(x,y): • Value of max(3,0) ? """Returns: max of x, y""" A: 3 CORRECT # swap x, y B: 0 # put the larger in y C: Error! if x > y: D: I do not know temp = x x = y • Local variables last until y = temp § They are deleted or § End of the function return temp • Even if defined inside if 9/17/15 Conditionals & Control Flow 21

  22. Program Flow vs. Local Variables def max(x,y): • Value of max(0,3) ? """Returns: max of x, y""" A: 3 # swap x, y B: 0 # put the larger in y C: Error! if x > y: D: I do not know temp = x x = y y = temp return temp 9/17/15 Conditionals & Control Flow 22

  23. Program Flow vs. Local Variables def max(x,y): • Value of max(0,3) ? """Returns: max of x, y""" A: 3 # swap x, y B: 0 # put the larger in y C: Error! CORRECT if x > y: D: I do not know temp = x x = y • Variable existence y = temp depends on flow • Understanding flow return temp is important in testing 9/17/15 Conditionals & Control Flow 23

  24. Program Flow and Testing # Put max of x, y in z • Must understand which flow caused the error print 'before if' § Unit test produces error if x > y: § Visualization tools show print 'if x>y' the current flow for error z = x • Visualization tools? else : § print statements print 'else x>y' § Advanced tools in IDEs z = y (Integrated Dev. Environ.) print 'after if' 9/17/15 Conditionals & Control Flow 24

Recommend


More recommend