1 / 19

Conditions we can Program With…

Conditions we can Program With…. Dr. Paige H. Meeker. Conditionals. If it’s raining, use an umbrella. If a<b, c=10. Otherwise, d=20. If the dog barks, let it outside. If the tub is full, turn off the water. Execute statements when Boolean expression is true. Boolean Expressions.

steffi
Télécharger la présentation

Conditions we can Program With…

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Conditions we can Program With… Dr. Paige H. Meeker

  2. Conditionals If it’s raining, use an umbrella. If a<b, c=10. Otherwise, d=20. If the dog barks, let it outside. If the tub is full, turn off the water. • Execute statements when Boolean expression is true

  3. Boolean Expressions 1 + 1 == 2 “CS1301” == “CS1301” 4 < 6 “A” < “L” “A” <= “L” 5.0 != 6.0 • Evaluate to True or False • Comparison and Equality Operations • == Equals • != Not equal • < Less than • > Greater than • <= Less than or equal to • >= Greater than or equal to

  4. Logical Operators • Evaluate to True or False • Combine Other Boolean Values • And - both must be true • Or - either evaluates to true • Not - inverts the truth value

  5. What do the following evaluate to? (True and True) evaluates to ???? (True and (3 == 2)) evaluates to ??? (False and False) evaluates to ??? (True or True) evaluates to ??? (True or False) evaluates to ??? ((‘a’ > ‘z’) or False) evaluates to ??? not True evaluates to ??? not False evaluates to ??? not (3 < 2) evaluates to ???

  6. What do the following evaluate to? (True and True) evaluates to True (True and (3 == 2)) evaluates to False (False and False) evaluates to False (True or True) evaluates to True (True or False) evaluates to True ((‘a’ > ‘z’) or False) evaluates to False not True evaluates to False not False evaluates to True not (3 < 2) evaluates to True

  7. Conditionals • Execute statements when expression is true • Boolean expressions • “If” keyword • Group of statements is called a block if (getBattery() < 6): print “Battery Low” x = 23 if (x != 0): print “Nonzero Number” x = 23 if (x > 0 or x < 0): print “Nonzero Number” password = “robots” v = raw_input(“Enter password”) if (v == password): print “permission granted Indent

  8. Otherwise … • Execute a block statements when expression is true another block when false • “else” keyword if (getBattery() < 6): print “Battery Low” else: print “Battery OK” x = int(raw_input(“number: “)) if (x != 0): print “Nonzero Number” else: print “Zero!” Indent

  9. 2 Note Flute def flute(): if(getLight(“left”) > getLight(“right”)): beep(1, 440) #a4 else: beep(1, 494)#b4 • 2 note flute • Play A when holding left light sensor • Play B when holding right light sensor • Higher values mean less light

  10. Nested Conditionals • “If” statements can be nested • Treated like any other statement • getBattery() 3? • getBattery() 5? • getBattery() 4? • getBattery() 7? • getBattery() 6? if (getBattery() < 6): print “Battery Low” if (getBattery() > 4): print “Not too Low”

  11. Nested Conditionals • “If” statements can be nested • If/else indentation must match! if (getBattery() < 6): print “Battery Low” if (getBattery() > 4): print “Not too Low” else: print “VERY Low” else: print “Battery OK CS1301 - O'Hara

  12. String Matching weather = raw_input(“What’s the weather like?“) if (weather == “rain”): print “Grab your Umbrella” if (weather == “sunny”): print “Grab your shades” print “Don’t forget the suntan lotion” if (weather == “snow”): print “Wow! In SC??” print “GDH – Hide the trays!” • Can’t be all those names at the same time • redundant • What if we want a “catch-all” else • where should it go?

  13. Chained Conditionals weather = raw_input(“What’s the weather like?“) if (weather == “rain”): print “Grab your Umbrella” else: if (weather == “sunny”): print “Grab your shades” print “Don’t forget the suntan loation” else: if (weather == “snow”): print “Wow! In SC??” print “GDH – Hide the trays!” else: print “Blink and it will change.” • Mutually exclusive conditions • Just one conditinal to be true

  14. Chained Conditionals Shortcut • Only one block will be executed weather = raw_input(“What’s the weather like?“) if (weather == “rain”): print “Grab your Umbrella” elif (weather == “sunny”): print “Grab your shades” print “Don’t forget the suntan lotion” elif (weather == “snow”): print “Wow! In SC??” print “GDH – Hide the trays!” else: print “Blink and it will change.”

  15. Chained Conditionals Short-er-cut • Only one block will be executed • Use “or” logical operation Weather = raw_input(“What’s the weather like?“) if (weather == “rain”): print “Grab your umbrella” elif (weather == “icy” or weather == “snow”): print “Watch out!” else: print “Blink and it will change!”

  16. Checking the Robots Vitals if (getBattery() > 6.8): print “I’m good as new” if (getBattery() > 6.0): print “I’m healthy” if (getBattery() > 5.0): print “On my way out” else: print “Dead robot walking” • Under what condition does each statement print?

  17. Checking the Robot’s Vitals (Again) if (getBattery() > 6.8): print “I’m good as new” elif (getBattery() > 6.0): print “I’m healthy” elif (getBattery() > 5.0): print “On my way out” else: print “Dead robot walking” What’s the difference between this code and the code on the last slide?

  18. 3 Note Flute def flute (): l = getLight(“left”) c = getLight(“center”) r = getLight(“right”) if (l > r and l > c): beep(1, 440) #a4 elif (c > l and c > r): beep(1, 494) #b4 else: beep(1, 523) #c4 • 3 note flute • Uses chained conditionals • 3 mutually exclusive events CS1301 - O'Hara

  19. Things to try… • Create a function that accepts user input of a number. • If the number is less than 10, move the robot forward for that number of seconds. • If the number is greater than 10, move the robot backward for 5 seconds • If the number is equal to 10, turn the robot for 10 seconds. • Email your functions to me before Wednesday.

More Related