Download presentation
Presentation is loading. Please wait.
1
Thinking about if’s
2
How is your problem expressed?
A. Are you looking for specific values and nothing else is of interest? B. Are you dividing a range up into specific segments, so that everything is of interest? C. Are you testing for conditions that are mutually exclusive? D. Or conditions that can overlap?
3
Specific values if x == 5: print(“do the 5 thing”) elif x == 19:
#NO else here! Nothing else to do!
4
Dividing a range if x > 100: print(“wonderful!”)
else if x >= 70: print(“ok” ) else if x >= 55: print(“eh”) else: print(“bleah”) #need the last else to catch “everything else”
5
Mutually exclusive if x % 10 == 5:
print(“that’s a number ending in 5”) else: print(“not a 5 on the end!”) # do NOT need “else if (x % 10 != 5)” # if the first test is false, this one must be # true
6
Overlapping conditions
if x > 50: print(“too high!”) if x % 2 == 0: print(“that’s even!”) # two separate if’s, because the tests are # independent of each other
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.