Compound Boolean Expressions¶
Just before we looked at the If … Then statement we looked at Boolean expressions. Boolean expressions have two and only two potential answers, either they are true or false. So far we have looked at just simple boolean expression, with just one comparison. A boolean expression can actually have more than one comparison and be quiet complex. A compound boolean expression is generated by combining more than one simple boolean expression together with a logical operator And or Or. And is used to form an expression that evaluates to True only when both operands are true. Or is used to form an expression that evaluates to true when either operand is true. Here is a truth table for each operator:
AND Truth Table
A |
B |
A AND B |
|---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
OR Truth Table
A |
B |
A OR B |
|---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
In some programming languages the operators are simpley the words “AND and OR”. In others they are “&&” for AND and “||” for OR. The following are some examples of compound boolean expressions:
Besides these two logical operators, there is one more, the NOT. NOT is used most often at the beginning of a Boolean expression to invert its evaluation. It does not compare 2 values but just inversts a single one.
NOT Truth Table
A |
NOT(A) |
|---|---|
True |
False |
False |
True |
For example: