not
In the first section of this chapter, we talked about the logical operators and
and or
. There is one that we left out–not
.
not
acts a little bit differently than and
and or
.
We can think of and
and or
as taking two boolean values and combining them into one boolean value.
The not
logical operator takes one boolean value and switches it to the opposite value.
The place where this gets complicated is when we use not
in combination with other comparison and logical operators. This is because the portion of the logical expression affected by not
is not always obvious.
Make a guess as to how the following expressions will evaluate before running them.
not
is going to only apply to the first boolean value to the right of it (unless you use parentheses).
not
’s precendence is going to be higher than and
and or
but lower than our comparison operators.