Write a piece of R code that you could use to teach a seven-year-old child about Boolean logic. If you have never encountered Boolean logic before (sometimes it is called Boolean algebra), you will have to look it up to discovervthe three essential operations (AND, OR, NOT) and how they work. In addition, you will have to discover the “operators” (i.e., the special punctuation marks) that R uses to represent Boolean operations.

For example, here is a single line of code (and the response from the R-console in bold) that represents one of the two possible outcomes of the AND operator:

1 & 1
1 & 1
## [1] TRUE

Take note of several important aspects of this example: 1) It has a comment that explains a little bit of what is going on (all of the stuff after the # character); 2) it needs more comments if it is going to be helpful to a seven-year-old; and 3) more lines of code are needed to demonstrate the other outcome of AND, as well as all of the outcomes of OR and NOT. Keeping your seven-year-old in mind, write and submit the rest of the code and comments.

Then use these conditional statements within an if statement, to printout the expected logic. For example:

#show the use of an ‘if’ statement - that 1&1 is true
if( 1&1 ) print(”1&1 is true”) else print(“error somewhere”)
if( 1&1 ) print("1&1 is true") else print("error somewhere")
## [1] "1&1 is true"
# let's assume we have some sheep and we're trying to get them to go through the gate that matches their fleece. (From Denise Devine, Summer 2018)

#assign some fleece colors to sallysheep and mollysheep
sallysheep = "black"
mollysheep = "white"

#now we assign colors to the gates
gate1 = "black"
gate2 = "white"

# Lets evaluate using a few conditionals
if(sallysheep == gate1) print ("You match you may enter") else print ("Halt") 
## [1] "You match you may enter"
if(sallysheep == gate1 & mollysheep == gate2) print ("You match you may enter") else print ("Halt")
## [1] "You match you may enter"
if(sallysheep == gate1 || sallysheep == gate2) print ("You match you may enter") else print ("Halt")
## [1] "You match you may enter"
if(sallysheep == gate2) print ("You match you may enter") else print ("Halt")
## [1] "Halt"

Learning Goals for this activity:

A. Making sure that you have R running properly on your computer.
B. Familiarity with the command line (console) interface of R.
C. Understanding of the foundation of “conditionals” in programming: Boolean logic/algebra.
D. Developing informative commenting practices.