The simplest data type!
George Boole, mid 19th century English mathematician and logician
true & false
yes & no
on & off
etc.
if
…if (<boolean>) {
// do something
}
Yeah, we haven’t learned about if
yet.
while
while (<boolean>) {
// do something
}
Haven’t learned about while
yet, either.
We’ll get to it.
They are just another kind of value to compute with.
As with numbers, we can write boolean values directly in our programs as follows.
true
false
Unlike numbers, there aren’t different ways of writing the same literal value.
Same as with numbers: combine them in expressions.
But the operators are different.
AND, OR, and NOT
These are the three operators defined in Boolean Algrebra by our friend George Boole.
We’re all used to this. Sally and Joey are both short. Sally is also funny but Joey is not.
Yes.
No.
&&
short && funny
Evaluates to true if, and only if, both of its operands are true.
We’re also used to this. Same as before. Sally is short and funny. Joey is short and not funny. And Sam is tall and not funny.
Yes.
Yes.
No.
||
short || funny
Evaluates to true if either, or both, operands are true.
Another one just like English.
No.
Yes.
!
!funny
Flips the logical value, true to false and false to true.
Suppose hungry
is a boolean that says whether I’m hungry and angry
says whether I’m angry.
What’s an expression that captures whether or not I’m hangry?
hungry && angry
Suppose homework
is a boolean that says whether I have homework to grade and newEpisodes
is a boolean that says whethere there new episodes of my favorite TV show available.
What’s an expression that captures whether I will stay up late if I always stay up late to grade homework or to watch new episodes of my favorite show?
homework || newEpisodes
asleep
is a boolean that says whether I’m asleep.
What’s an expression that says whether I’m awake?
!asleep
Booleans are just another kind of value.