Computer Science 15-112, Spring 2012
Class Notes:  Conditionals

Conditionals

1. Python Language Reference, Ch 7.0 (Compound statements introduction)
Be sure to understand the modified BNF and the syntax it describes!
2. Python Language Reference, Ch 7.1 (The if statement)
Ditto!

2. The "if" statement
1. "if'

if (1 < 2):
print "yes!"
if (5 < 4):
print "really?"
print "you bet!"

2. "if-else"

if (5 < 4):
print "really?"
else:
print "not really!"
print "fascinating!"

3. "if-elif-...-else"

if (5 < 4):
print "really?"
elif (4 < 3):
print "curious!"
elif (3 > 2):
print "perhaps..."
else:
print "why not?"
print "yippee!"

A Better Example:

n = int(raw_input("Enter an integer: "))
if (n > 0):
print n,"is positive"
elif (n == 0):
print n,"is zero"
else:
print n,"is negative"

Aside:  Same example with a nasty bug:

n = raw_input("Enter an integer: ")
if (n > 0):
print n,"is positive"
elif (n == 0):
print n,"is zero"
else:
print n,"is negative"

4. Nested "if" statements
`if (2 < 1):    if (3 == 3):        print "a"    else:        print "b"else:    if (4 == 4):        print "c"    else:        print "d"`
5. The "dangling-else" problem
`print "first:"if (2 < 1):    if (3 == 3):        print "a"    else:        print "b"        print "second:"if (2 < 1):    if (3 == 3):        print "c"else:        print "d"`
3. Complex Conditionals and Boolean Logic
`age = 42if ((age >= 6) and (age <= 21)):    print "student"elif (not ((age < 6) or (age > 75))):    print "professional"`
Same logic, after applying DeMorgan's Law:
```age = 42
if ((age >= 6) and (age <= 21)):
print "student"
elif ((age >= 6) and (age <= 75)):
print "professional"```
Same logic again, sharing a common condition:
```age = 42
if (age >= 6):
if (age <= 21):
print "student"
elif (age <= 75):
print "professional"```

And why we parenthesize...:

```print not True and False
print not (True and False)
```
4. The pass statement
`if (2 < 1):    passelse:    print "sometimes you may need pass, but this is not one of them!"`
5. Conditional Expressions
`x = 3 if (1 < 2) else 4print xx = 5 if (1 > 2) else 6print x`
A Somewhat Better Example:
`p = 5print "I saw",p,"people" if (p>1) else "person"p = 1print "I saw",p,"people" if (p>1) else "person"`
6. Incorrect usage

1. Negated Condition (with "else" clause)

 Wrong Right b = Trueif (not b):     print "no"else:    print "yes"Or:b = Trueif (b == False):     print "no"else:    print "yes" b = Trueif (b):     print "yes"else:    print "no"Or:b = Trueif (b == True):     print "yes"else:    print "no"

2. Empty "if" Clause

 Wrong Right b = Falseif (b):    passelse:    print "no" b = Falseif (not b):    print "no"Or:b = Falseif (b == False):    print "no"

3. Using "if" instead of "and"

 Wrong Right b1 = Trueb2 = Trueif (b1):     if (b2):        print "both!" b1 = Trueb2 = Trueif (b1 and b2):     print "both!"

4. Avoiding "else"

 Wrong Right b = Trueif (b):    print "yes" if (not b):    print "no" b = Trueif (b):    print "yes" else:    print "no"

Another Example:

 Wrong Right x = 10if (x < 5):    print "small" if ((x >= 5) and (x < 10)):    print "medium" if ((x >= 10) and (x < 15)):    print "large" if (x >= 15):    print "extra large" x = 10if (x < 5):    print "small" elif (x < 10):    print "medium" elif (x < 15):    print "large" else:    print "extra large"

Yet Another Example:

 Wrong Right c = 'a'if ((c >= 'A') and (c <= 'Z')):    print "Uppercase!" if ((c >= 'a') and (c <= 'z')):    print "lowercase!" if ((c < 'A') or \    ((c > 'Z') and (c < 'a')) or \    (c > 'z')):    print "not a letter!" c = 'a'if ((c >= 'A') and (c <= 'Z')):    print "Uppercase!" elif ((c >= 'a') and (c <= 'z')):    print "lowercase!" else:    print "not a letter!"

5. Using boolean logic instead of if
 Wrong Right x = 42 y = ((x > 0) and 99) x = 42 if (x > 0):     y = 99

Another example:

 Wrong Right x = 42 y = (((x > 0) and 99) or      ((x < 0) and 88) or      77) x = 42 if (x > 0):     y = 99 elif (x < 0):     y = 88 else:     y = 77

6. Using boolean arithmetic instead of if

 Wrong Right x = 42 y = ((x > 0) * 99) x = 42 if (x > 0):     y = 99 else:     y = 0 # or, if you prefer... y = 99 if (x > 0) else 0

7. Practice problems

Write the following methods:   isEven, isOdd, isPositive, ...  Maybe solve hw1 problems using conditionals....

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem