RU beehive logo ITEC dept promo banner
ITEC 109
2014spring
ibarland

homeinfolabsexamshws
D2LMediaSamples/breeze (snow day)tutor/PIs

lab37
and,or

Yesterday, we walked through examples of using and and or:

// Return the RU Username, except check for Jay Z who gets a special honorific.
//
def ruUserName( firstName, lastName ):
  if firstName == "Jay" and lastName == "Z":
    return "hisroyalflyness"
  else:
    firstInit = substring(firstname,0,1)
    return string.lower( firstInit + lastName )
    
// Charge $4.95 for shipping, unless the order is worth more than $100 total,
// or it has more than 50 items (in which case shipping is free).
// 
def shippingCharge( numItems, unitPrice ):
  if (numItems*unitPrice > 100  or  numItems > 50):
    return 0.00
  else:
    return 4.95
    

The precise semantics for and, or, not are what you'd expect; here they are in table form1:

    
FalseandFalse == False
FalseandTrue == False
True andFalse == False
True andTrue == True
FalseorFalse == False
FalseorTrue == True
True orFalse == True
True orTrue == True
notFalse == True
notTrue == False
Note that you can actually pass/return the boolean values True and False to/from functions (just like you can pass/return particular numbers, or particular strings).

TASKS: we will check off the following two items:


1If you replace False,True with 0,1, what arithmetic function is similar to and? To or? …To not?      

2 Technically, I'd split hairs over the distinction of "not having a first name" vs. "having a first name which is zero letters long". Granted, this isn't actually a distinction with people's names, but consider the situation where you look up a (last)name in a database: there is a difference between Sting not being in the database, vs. Sting being there but not having a last name.

A full programmer might say that “no last name” is represented by False, while a string (even the empty-string) represents a known last-name. Using False like this is called a “sentinel value”.

     

homeinfolabsexamshws
D2LMediaSamples/breeze (snow day)tutor/PIs


©2014, Ian Barland, Radford University
Last modified 2014.Apr.14 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme