In the previous sections, we have seen how the Boolean operators and Comparison operators can be used to form expressions. In this section, we combine the two types of operators to form more complex expressions.
For example, suppose we ask the question: Are there more than 30 students in the class, and are there more than 30 seats in the room? This might be expressed as:
Number_of_Students > 30 AND Number_of_Seats > 30
Number_of_Students > 30 AND Number_of_Seats > 30
We evaluate such an expression by determining the value (True or False) of each of the comparison expressions, and then use those values to evaluate the Boolean expression. In our example, suppose the class is a very large one that is held in a large room. In other words, there are more than 30 students and the room has more than 30 seats. Therefore, we would evaluate the expression as follows:
- The Number_of_Students > 30 portion of the expression evaluates to True.
- The Number_of_Seats > 30 portion of the expression evaluates to True.
- Substituting those values in the expression give us:
True AND True - The True AND True expression evaluates to True as can be seen in the Truth tables for the AND operator.
- Therefore, we conclude that the entire expression evaluates to True.
The following example expressions are based on these assumptions:
- All employees belong to a department
- All employees working in department 5 have salaries greater than $25,000
- All employees in department 4 make exactly $40,000 per year.
- Alice is an employee and she works in department 5
- Bill is an employee and he works in department 4
Given the above 5 items are facts, consider the truth values of the following expressions:
Expression | Evaluation |
---|---|
Alice_salary < 25000 AND Alice_Department = 5 | Alice_salary < 25000 is False Alice_Department = 5 is True False AND True is False |
NOT Alice_salary < 25000 | Alice_salary < 25000 is False NOT False is True |
Alice_salary < 25000 OR Bill_Salary = 40000 | Alice_salary < 25000 is False Bill_Salary = 40000 is True False OR True is True |
Alice_salary > 25000 AND NOT Bill_Salary < 40000 | Alice_salary > 25000 is True Bill_Salary < 40000 is False NOT False is True True AND True is True |
As an exercise, evaluate the following expressions:
Expression | Evaluation |
---|---|
Alice_salary < 25000 OR Alice_Department = 4 | |
Alice_salary >= 25000 AND Bill_department = 4 | |
Alice_salary < 25000 OR Bill_Salary < 40000 OR Alice_Department = 5 | |
NOT ( Alice_salary > 25000 AND Bill_Salary < 40000 ) | |
Alice_salary < 25000 AND ( Bill_Salary <= 40000 OR Alice_Department = 5 ) |
Notice in the last two cases, parenthesis may be used to group portions of the expression. In general Boolean expressions are evaluated from left to right except where parenthesis change the order.
In the third example above, notice that when multiple conditions are separated by OR, it only takes one of the conditions to be True in order to cause the entire expression to evaluate to True.