We can also have expressions that represent values which are either
true or false. These expressions are called Boolean expressions. Usually
such expressions involve the comparison of values with the following
operators: >
(greater than), <
(less than), and =
(equals). For example, consider the following three Boolean expressions:
(6
> 10) 
(6
> 6) 
(6
= 10) 
(6
< 10) 
Languages such as C++ that use the = for the
assignment operation often use == (double equals)
to test for equality. Be careful not to confuse the two when programming
in these languages.
We can also have some boolean expressions with two operators that compare
relative size and equality. These two operators are >=
(greater than or equal to) and <=
(less than or equal to).
(6
>= 10) 
(6
>= 6) 
(6
<= 10) 
Since these are Boolean expressions, each statement is either true
or false. Of course, the answers are quite easy to determine because
we know the relationship between 6 and 10. However, Boolean expressions
can also be constructed from variables like the following one:
Without knowing the value of the variable count
we cannot determine the value of the Boolean expression. Quite often
an expression like this is used to control a repetitive action. Each
time the action is performed, the value of count
increases until the expression eventually becomes false. We will see
more about this kind of control in a few more lessons.
Boolean expressions can be joined together to form longer expressions
using three logical operators: AND,
OR,
and NOT. Suppose
we want to test our count
variable to determine if the count is between 10 and 100. We can write
a Boolean expression using the AND
operator to construct our test:
(count
> 10) AND (count
< 100) 
In order for our entire Boolean expression to be true, both of the
smaller expressions must be true. If count
is equal to 1000, then only the first half of the expression is true,
and the entire expression evaluates to false. This is exactly what we
want because 1000 is not between 10 and 100. If count is equal to 1,
then only the second half of the expression is true, and the entire
expression evaluates to false. This is also what we want since 1 is
also not between 10 and 100.
Now let's consider how we could write a Boolean expression to test
whether count
is outside the range 10 to 100. One easy solution would be to reverse
the test we have already created using the NOT
operator. This operator turns true values to false and false values
to true. Therefore, if we want to construct the opposite test, we can
reverse the test we already have like this:
NOT ((count
> 10) AND (count < 100)) 
Although constructing this new test only requires a small change to
our previous test, the meaning of the new test is not very clear. A
better way to construct this test is to use the OR operator. What we
really want to test is when count
is greater than 100 or less than 10.
(count <
10) OR (count > 100) 
In order for our entire Boolean expression to be true, at least one
of the smaller expressions must be true. If count
is equal to 1000, then the second half of the expression is true, and
our entire test is also true. This is exactly what we want because 1000
is not between 10 and 100. If count is equal to 1, then the first half
of the expression is true, and the entire test is also true. This is
also what we want since 1 is not between 10 and 100.
Often the truth values for the Boolean operators AND,
OR, and NOT
are described in a table called a truth table. The table lists all the
possible operands for a given operator and the resulting truth values.
Truth tables for AND,
OR, and NOT
are listed below. The values in parentheses represent a Boolean expression
that is either true (T) or false (F).
Boolean
Expression 
Truth
Value 
(T) AND (T) 
T 
(T) AND (F) 
F 
(F) AND (T) 
F 
(F) AND (F) 
F 

Boolean
Expression 
Truth
Value 
(T) OR (T) 
T 
(T) OR (F) 
T 
(F) OR (T) 
T 
(F) OR (F) 
F 

Boolean
Expression 
Truth
Value 
NOT (T) 
F 
NOT (F) 
T 

To summarize, the AND
operation is only true when it joins two true Boolean expressions. The
OR operation
is only false when it joins two false expressions. The NOT
operation always reverses the truth value of the expression.