Java Reference
In-Depth Information
This expression is a combination of two Boolean statements. The statement
“It is raining out today” is either true or false. The statement “x is equal to 4”
is either true or false. The and combines these two Boolean expressions into a
single, larger expression that is either true or false.
We use what is called a truth table when determining whether a combina-
tion of Boolean expressions is true or false. In the case of the and operator, the
only time the larger expression is true is when both smaller expressions are
true, as shown in Table 3.1.
Therefore, the earlier comment about the rain and variable x is only true
when it is actually raining out and x is indeed the value 4. If x is 4, but it is not
raining out, the entire expression becomes false. Similarly, if it is raining but x
is not 4, the expression is false. Finally, if it is not raining and x is not 4, the
entire expression is again false.
Just like the old saying that two wrongs don't make a right, two false
statements do not make a true statement. An expression of the form
false and false is false.
The or Operator
Suppose that I make the following comment:
Today is Monday or I will buy lunch for everyone today.
With the or operator, if either of the two expressions is true, the entire
expression is true. Therefore, in my statement about Monday and lunch, if it is
indeed Monday, I do not need to take anyone to lunch and the statement is still
true. Similarly, if I buy lunch for everyone and it is not Monday, the statement
is true. If it is not Monday, and I do not buy lunch for anyone (which is the
likely outcome), the statement is false.
Table 3.2 shows the truth table for the or operator. Notice in Table 3.2 that the
only time an or expression is false is when both of the statements in the expres-
sion are false.
Table 3.1
The Truth Table for the and Operator
AND
TRUE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
Search WWH ::




Custom Search