190 likes | 317 Vues
Dive into the essential concepts of conditionals and boolean expressions in programming with Dr. Paige H. Meeker. This guide explores how to execute statements based on boolean conditions, evaluate true or false expressions, and utilize logical operators such as AND, OR, and NOT. Learn through practical examples, including nested conditionals and chained conditionals, to understand how programs respond to varying conditions, such as weather scenarios. Enhance your programming skills with clear explanations and real-world applications.
E N D
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 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
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
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 ???
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
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
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
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
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”
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
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?
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
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.”
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!”
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?
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?
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
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.