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 quite complex. A compound Boolean expression is generated by combining more than one simple Boolean expression together with a logical operator NOT, AND & OR. NOT is used to form an expression that evaluates to True only when the operand is false. 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:
NOT Truth Table
A |
NOT A |
---|---|
True |
False |
False |
True |
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 |
The general form in a programming language for a compound Boolean expression is:
NOT
AND
OR
In some programming languages the operators are simply the words not, and & or. In others they are “!” for NOT, “&&” for AND & “||” for OR.
In this example program, the user is asked to enter a term mark and a final project mark. The program then tells the user if they passed the course or not. The rule for passing the course is that the student must have a term mark of at least 50% and a final project mark of at least 50. The program uses a compound Boolean expression to determine if the student passed the course.