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.
What values can be represented?
What is the syntax for writing them in our langauge?
What can we do with them?
As we said before, true and false.
It doesn’t matter.
I literally do not know.
They are fully abstracted.
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.
Writen &&
true && false
Evaluates to true if, and only if, both of its operands are true.
Written ||
true || false
Evaluates to true if either, or both, operands are true.
Written !
!true
Flips the logical value, true to false and false to true.
Writing expressions with all literal values (true
and false
) is kinda silly because we could just figure out what the value is and write that.
But they make a lot more sense when we are writing expressions in terms of named values.
We’ll talk a lot more about variables later.
For now just know that we can use a name to refer to a value in the place of a literal value.
E.g.
happy = true
Some examples.
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
===
!==
<
>
<=
>=
But it’s very rarely needed.
And definitely don’t compare the value of a boolean expression to a boolean literal.
x === true
just write:
x
(x === true) === true
Or maybe?
((x === true) === true) === true
🤔
x === false
or
x !== true
just write:
!x
Booleans are just another kind of value.