conditionals conditionals if statements format example
play

Conditionals Conditionals: If-Statements Format Example if < - PowerPoint PPT Presentation

Mini-Lecture 11 Conditionals 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


  1. Mini-Lecture 11 Conditionals

  2. 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/19/18 Conditionals 2

  3. 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/19/18 Conditionals 3

  4. 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/19/18 Conditionals 4

  5. Conditionals and Local Variables • temp is needed for swap def max(x,y): """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 4 3 x = y 4 y = temp x y 0 0 temp 3 5 return y 9/19/18 Conditionals 5

  6. Conditionals and Local Variables • temp is needed for swap def max(x,y): """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 5 3 x = y 4 y = temp x y 0 3 temp 3 5 return y 9/19/18 Conditionals 6

  7. Conditionals and Local Variables • temp is needed for swap def max(x,y): """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/19/18 Conditionals 7

  8. Conditionals and Local Variables • Value of max(3,0) ? def max(x,y): """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/19/18 Conditionals 8

  9. Conditionals and Local Variables • Value of max(3,0) ? def max(x,y): """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/19/18 Conditionals 9

  10. Conditionals and Local Variables • Value of max(0,3) ? def max(x,y): """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/19/18 Conditionals 10

  11. Conditionals and Local Variables • Value of max(0,3) ? def max(x,y): """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/19/18 Conditionals 11

  12. Conditionals: If-Elif-Else-Statements Format Example if < boolean-expression >: # Put max of x, y, z in w < statement > if x > y and x > z: … w = x elif < boolean-expression >: elif y > z: < statement > … w = y … else : else : w = z < statement > … 9/19/18 Conditionals 12

  13. Conditionals: If-Elif-Else-Statements Format Notes on Use if < boolean-expression >: • No limit on number of elif < statement > § Can have as many as want … § Must be between if , else elif < boolean-expression >: • The else is always optional < statement > § if - elif by itself is fine … … • Booleans checked in order else : § Once it finds a true one, it skips over all the others < statement > … § else means all are false 9/19/18 Conditionals 13

  14. Conditional Expressions Format Example # Put max of x, y in z e1 if bexp else e2 z = x if x > y else y • e1 and e2 are any expression • bexp is a boolean expression • This is an expression! expression, not statement 9/19/18 Conditionals 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend