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
}
Covered in Unit 3
while
while (<boolean>) {
// do something
}
Covered in Unit 4
for
for (int i = 0; <boolean>; i++) {
// do something
}
Also covered in Unit 4
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.
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 using them as we saw in the warmup.
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 variables.
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.