We now know how to write simple "if-then" statements in Snap! So far, the conditional in our uses of the
"if" block have been single built-in blocks (like ). Let's take a look at a few more complex examples:
In past examples, we've seen conditionals that contain a single predicate (,
, etc.). We could write a single block for each of the above conditionals, but that might be a bit
weird. In the first sentence, for example, this would entail writing an "am hungry and with friends?"
predicate. This seems a little strange because the "am hungry" and the "with my friends" parts aren't
necessarily related; it doesn't make sense to put them as a single predicate. So, instead, we might write
separate "am hungry?" and "with friends?" predicates and combine them in some way. This brings us to three
special predicates:
and
, or
, and not
.
And
, or
, and not
are all predicates that take in other
predicates. These three predicates behave in ways you might expect from their meanings in English. When will
the phrase "I am hungry and with my friends" evaluate to true? Only when both "I am hungry" and "I am with
my friends" are true. If I wasn't with my friends, the entire phrase would certainly be false. We often
summarize the behavior of predicates like and
, or
, and not
using what
are called truth tables. Here is the truth table for and
:
A | B | A and B |
---|---|---|
F | F | F |
F | T | F |
T | F | F |
T | T | T |
Here, A and B are the two inputs to the and
block. The and
block only accepts
boolean values, so A and B are either true
(T) or false
(F). Reading across each
row tells us what A and B
will output, given particular values of A and B. Here are the truth
tables for or
and not
:
A | B | A or B |
---|---|---|
F | F | F |
F | T | T |
T | F | T |
T | T | T |
A | not A |
---|---|
F | T |
T | F |
Do these definitions fit with what you'd expect from English? Something you should notice is that "A or B" is still true even if both A and B are true. You might be tempted to argue that "or" should be false in this case (consider the sentence "you may have hashbrowns or toast with your breakfast;" having both is generally assumed not to be an option). However, when "or" is used in a conditional, we assume that it is "inclusive;" that is, it includes the fourth case as true (think about the third example from earlier; if we were out of both eggs and milk, we'd almost certainly still want to go to the store). The exclusive version of "or," often called "xor," is available in many other programming languages, but it isn't included in Snap!