Writing Boolean Expressions Boolean expressions can become complex. A single expression may test several conditions. It may also provide several outcomes. When writing complex expressions, try to break them down into small steps. Use operators to join conditions or outcomes. The AND operator can be used to combine two conditions into a single Boolean expression. The expression is true only if both conditions are true. For example: IF today is Tuesday AND it is morning This is a combination of the conditions: IF today is Tuesday IF it is morning Each of these conditions can be either true or false. If both conditions are true, the overall Boolean expression is true. If either condition is false, or if both conditions are false, the overall Boolean expression is false. Suppose it is Tuesday at 10:00 a.m. The condition IF it is Tuesday is true. The condition IF it is morning is also true. Therefore, the overall Boolean expression is true. Suppose it is Tuesday at 1:00 p.m. The condition IF it is Tuesday is true, but the condition IF it is morning is false. Therefore, the overall Boolean expression is false. The OR operator can also be used to combine two conditions into a single Boolean expression. However, unlike with the AND operator, if either condition is true, the overall Boolean expression is true. For example: IF today is Tuesday OR it is morning Suppose it is Tuesday at 3:00 p.m. The condition IF it is Tuesday is true, but the condition IF it is morning is false. Because at least one of the conditions is true, the overall Boolean expression is true. Suppose it is Wednesday at 5:00 p.m. The condition IF it is Tuesday is false. The condition IF it is morning is also false. Since both conditions are false, the overall Boolean expression is false. Hands-On Example 5.2 Hands-On Example 5.2 Boolean Expression p Construction It is important to o understand how Boolean expre es sions are evaluated. This will help when creating Boolean expressions. p Complex Boolean expr re ssions can be constructed with the and and or operator block ks . 1 Consider this ex xp ression: = + 5 3 2 2 2 Goodheart-Willcox Publisher Can this expression s be evaluated as true or false, or is there not enough information? Since the values are hard-coded, the expression can be e evaluated. It is true because 3 added to 2 does indeed equ ua l 5. Continued Copyright Goodheart-Willcox Co., Inc. 106 Introduction to Computer Science: Coding