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

IF not(Boolean expression #1) THEN
Statements to be performed …
ELSE
Statements to be performed …
ENDIF

AND

IF ((Boolean expression #1) and (Boolean expression #2)) THEN
Statements to be performed
ELSE
Statements to be performed …
ENDIF

OR

IF ((Boolean expression #1) or (Boolean expression #2)) THEN
Statements to be performed
ELSE
Statements to be performed …
ENDIF

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.

Top-Down Design for Compound Boolean Expression statement

Top-Down Design for Compound Boolean Expression statement

Flowchart for Compound Boolean Expression statement

Compound Boolean Expression flowchart

Pseudocode for Compound Boolean Expression statement

GET term_mark
GET project_mark
IF ((term_mark >= 50) and (project_mark >= 50)) THEN
SHOW “You passed the course.”
ELSE
SHOW “You failed the course.”
ENDIF

Code for Compound Boolean Expression statement

Example Output

Code example output