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


  1. Required Reading
  2. The "if" statement
    1. "if'
    2. "if-else"
    3. "if-elif-...-else"
    4. Nested "if" statements
    5. The "dangling-else" problem
  3. Complex Conditionals and Boolean Logic
  4. The pass statement
  5. Conditional Expressions
  6. Incorrect usage
    1. Negated Condition (with "else" clause)
    2. Empty "if" Clause
    3. Using "if" instead of "and"
    4. Avoiding "else"
    5. Using boolean logic instead of if
    6. Using boolean arithmetic instead of if
  7. Practice problems

Conditionals

  1. Required Reading
    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 = 42
    if ((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):
    pass
    else:
    print "sometimes you may need pass, but this is not one of them!"
  5. Conditional Expressions 
    x = 3 if (1 < 2) else 4
    print x
    x = 5 if (1 > 2) else 6
    print x

    A Somewhat Better Example:
    p = 5
    print "I saw",p,"people" if (p>1) else "person"
    p = 1
    print "I saw",p,"people" if (p>1) else "person"

  6. Incorrect usage
     
    1. Negated Condition (with "else" clause)
       
      Wrong Right
      b = True
      if (not b):
          print "no"
      else:
          print "yes"

      Or:

      b = True
      if (b == False):
          print "no"
      else:
          print "yes"

      b = True
      if (b):
          print "yes"
      else:
          print "no"

      Or:

      b = True
      if (b == True):
          print "yes"
      else:
          print "no"

       

    2. Empty "if" Clause
       
      Wrong Right
      b = False
      if (b):
          pass
      else:
          print "no"
      b = False
      if (not b):
          print "no"

      Or:

      b = False
      if (b == False):
          print "no"

       

    3. Using "if" instead of "and"
       
      Wrong Right
      b1 = True
      b2 = True
      if (b1):
          if (b2):
              print "both!"
      b1 = True
      b2 = True

      if (b1 and b2):
          print "both!"

       

    4. Avoiding "else"
       
      Wrong Right
      b = True
      if (b):
          print "yes"
      if (not b):
          print "no"
      b = True
      if (b):
          print "yes"
      else:
          print "no"

      Another Example:

      Wrong Right
      x = 10
      if (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 = 10
      if (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