chapter 7 - spreadsheets: relational & boolean …web.cse.ohio-state.edu/cse1111/august...

23

Click here to load reader

Upload: vobao

Post on 17-May-2018

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

1.7 - RELATIONAL OPERATORS & BOOLEAN FUNCTIONS

Thus far, the formulas written in this text have used combinations of arithmetic operators and functions to produce a numeric value as a result. Using the concepts we’ve learned so far we can answer questions like “How much was last year’s income?” and “How much was this year’s income?” with numerical values. However, the arithmetic operators and functions we’ve learned so far do not allow us to answer questions like: “Was last year’s income greater than this year’s income?” In this chapter, we will learn operators and functions that allow us to decide whether or not a statement is true or false; we can then answer the previous question by deciding whether or not the phrase “Last year’s income is greater than this year’s income” is true. TRUE and FALSE are referred to as Boolean values and can be calculated using Excel formulas and functions.

Often when analyzing data, the decision about what to do next depends on whether or not the data meets specific criteria. For example, if we are calculating employees’ monthly paychecks, an employee may deserve a bonus if their sales exceeded a certain amount. In the previous chapter, the SUMIF and COUNTIF functions were used to count or sum data depending on whether or not a criterion was met. This chapter expands this ability to decide whether or not data meets criteria by introducing Relational operators and Boolean functions.

RELATIONAL OPERATORS

The simplest way to calculate a Boolean value is to use a relational operator. Relational operators compare two values to determine if the relational expression is TRUE or FALSE. Much like arithmetic operators (+, -, *, /, etc.), relational operators appear between two operands, which can either be numerical constants, Boolean values, text values, cell references, or nested formulas/functions. A relational expression is a relational operator with two operands. e.g., 3=3, 5>2, A4<=SUM(A8:A20), etc.

Note: Be careful to distinguish the values TRUE and FALSE from the labels “True” and “False”. As with numbers, the computer treats these differently in formulas.

USING RELATIONAL OPERATORS IN FORMULAS

To determine the total of three plus five in Excel, the formula =3+5 can be used. The formula begins with an equal sign and is followed by the appropriate operands and operator. To determine whether or not it is true or false that 3+5 is equal to 8 use a relational operator:

=3+5=8

Relational Operators & Boolean Functions 1.7 CSE1111 Page 81

Page 2: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

An ‘=8’ has been added to the formula. The first equal sign tells Excel that this is a formula, while the second equal sign is interpreted as a relational operator. Excel will first evaluate in order of precedence all parentheses, exponents and arithmetic operators before evaluating the relational operation. That is, the relational operators are last in the order of precedence (refer to Chapter 1-1 for a listing of the order of precedence). In this case the formula will first add 3 plus 5 to get 8, and then compare to see if =8=8. The resulting value will be TRUE.

There are six different relational operators that can be used. Examples of each are listed in the following table:

Table 1: Relational Operators

Description Operator

Example Resulting Value

Equal to = = 3+5=8 TRUENot equal to <> = Sum(3,7)<>10 FALSEGreater than > =100>Max(5,10,20) TRUELess than < = “B”<“A” FALSEGreater than or equal to

>= =B2=2 where B2 contains the value 2

TRUE

Less than or equal to

<= = 99<=8*3 FALSE

Be especially aware of the order of precedence in which these relational expressions are evaluated. As an example, the formula =B3^2<>10*Sum(A1:A10) would be evaluated as follows:

First the computer would evaluate SUM(A1:A10).

Next the computer would evaluate B3^2 (^ is the exponent symbol so B2^2 is the mathematical expression B32)

The result of SUM(A1:A10) would then be multiplied by 10.

Finally the two sides of the relational expression would be compared.

AN EXAMPLE

Consider the spreadsheet in Figure 1. This worksheet lists the cost of trips to Boston and New York broken down into the costs of Food, Lodging, and Travel. An initial budget amount is listed in cell B1.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 82

12

3456789

10

A B C Dtotal budget 1200

ItemNew York Boston

Boston > NY

Food 250 110 FALSELodging 300 189 FALSETravel 660 776 TRUE total 1210 1075 FALSE

Within budget FALSE TRUETravel the largest cost component TRUE TRUE

Figure 1

Page 3: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

Question #1: Write an Excel formula in cell D4, which can be copied down the column, to determine if this cost component is more for the Boston trip than the New York trip.

Compare the corresponding two values to get a Boolean value. i.e., is it true or false that 110 (food for Boston trip) is greater than 250 (food for NY trip)? This can be implemented using the greater than relational operator.

Translate into Excel syntax. The cost in Boston, C4, is greater than the cost in NY, B4. Hence, the formula would be =C4>B4. With relational operators, formulas can frequently be written in several ways; =B4<C4 is mathematical equivalent to =C4>B4.

Since the formula is being copied down the column, determine which references copy relatively and which copy absolutely. In this case both references copy relatively.

Question #2: Write an Excel formula in cell B9 that can be copied across the row to determine if the total cost of this trip is within budget. For convenience the cell B1 has been named budget.

Again, two values must be compared to get a Boolean logical result. Therefore a relational operator should be used. “Within budget” suggests that if the total value of this trip is less than or equal to the budget a TRUE value should be returned.

In Excel syntax, the formula is =B7<=budget. Note how the named range is being used this formula.

Since the formula is being copied across the row, consider which references copy relatively and which copy absolutely. The total value of the trip should change when the column is changed from NY to Boston (B7 becomes C7). The budgeted value remains the same so cell B1 should be referred to absolutely. In this case a previously assigned range name has been used to represent cell B1. Range names always copy absolutely, so no modifications are needed to this formula.

The final formula is =B7<=budget. If the range name were not used, the formula =B7<=$B1 would be required.

What not to do:

Note that placing an absolute sign in front of a ranged name ($budget) is incorrect syntax.

Question #3: Write an Excel formula in cell B10 that can be copied across the row to determine if travel is the largest component of the total cost for this trip.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 83

Page 4: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

Here, the travel cost must be compared to the largest cost item to determine if they are equal. Since we are checking whether two values are equal, the equal (=) relational operator will be required. In this case, the largest cost item has not yet been determined, so this also will have to be calculated as part of the formula.

In cell B12, the formula will begin with =B6, a beginning equal sign starting the formula and the value for travel to New York. This is then followed by a second equal sign for the relational comparison equal to. The last component of the equation is the “largest component of the total cost”. How can the largest cost component be determined from among the cells B4:B6? A MAX function finds the highest value from a list of values. The final formula would be =B6=Max(B4:B6).

This formula is being copied across the row. Each of these cell references (B4 and B6) change relatively when copied across (C4 and C6) so no changes to the formula are required.

COMPARING TEXT VALUES

Relational expressions can also be used to compare text strings. Text strings are compared using alphabetical order, where numbers come before letters. A valid Excel formula could be =“Big”>“Apple”. Since the letter B comes alphabetically after the letter A, this expression would be evaluated as TRUE. Here the quotes are necessary; if the formula were written as =Big>Apple the computer will look for the range named Big and compare it to the range named Apple. If both of these range names exist then the formula would compare the values contained in those named ranges. Otherwise a #NAME! error will be displayed.

What about the formula =“BIG”=“big”? In Excel, capital and lower-case letters are equivalent for comparison purposes so the result of =“BIG”=“big” is TRUE. Formulas with relational expressions can also reference cells containing text; the formulas would be written in the same way as if the cells contained any other value. The formula =AB200<Z25 would evaluate to FALSE if cell AB200 contained the text string “hello” and the cell Z25 contained the text string “goodbye”.

BOOLEAN FUNCTIONS

Relational operators can be used to compare two different values to determine if a relational expression is true or false. But how can one determine if all items in a group meet a specified criterion or if at least one item in a group meets a specified criterion? A list of Boolean values (TRUE/FALSE) can be evaluated using And, Or, & Not operations. For example is the value in cell B2 greater than 20 and the value in cell B3 greater than 20? In Excel these operations are performed using the functions AND, OR & NOT. These functions perform the following tasks:

Relational Operators & Boolean Functions 1.7 CSE1111 Page 84

Page 5: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

The AND function will evaluate a list of logical arguments (TRUE and FALSE values) and return TRUE if all of the arguments are TRUE. An AND function is FALSE if at least one of its arguments is FALSE.

The OR function will evaluate a list of logical arguments (TRUE and FALSE values) and return TRUE if at least one of the arguments is TRUE. The OR function is only FALSE if all of the arguments in the function are FALSE.

The NOT function will evaluate only one logical argument (either a TRUE or FALSE) return TRUE if it is FALSE. The NOT function essentially changes the value TRUE to FALSE or the value FALSE to TRUE.

THE AND FUNCTION SYNTAX

The AND function will evaluate a list of logical arguments to determine if they are all TRUE. Arguments may consist of any combinations of cell references, values, and ranges such that each reduce to a single TRUE or FALSE value. An AND function is FALSE if at least one argument is FALSE. The syntax of the AND function is as follows:

And(logical1, logical2,….).Consider the following examples:

Formula Value DescriptionAND(TRUE, TRUE, TRUE) TRUE The arguments include a list of Boolean

values directly entered as function arguments. Since all of the arguments are TRUE the final value is TRUE.

AND(25>24, 3<=2+1) TRUE The arguments contain nested Relational expressions. As 25>24 is TRUE and 3<=2+1 is TRUE the formula will be reduced to =AND(TRUE,TRUE) and then finally to TRUE.

AND(A1:A3) where cell A1 contains the value FALSE, and cells A2 & A3 contain the value TRUE

FALSE The argument listed is a range of cell references that contain TRUE/FALSE values. Since at least one logical argument is FALSE, the final value is FALSE.

AND(A1,A5<A4,MIN(A1:A5)=2) where cell A1 contains the value FALSE

FALSE The 1st argument is a cell reference to a cell with a Boolean value, the 2nd contains a nested relational expression containing cell references and the 3rd argument is a relational expression including a nested function. Since A1 contains the value FALSE, the result of the function is FALSE.

USING THE AND FUNCTION

Relational Operators & Boolean Functions 1.7 CSE1111 Page 85

Page 6: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

Consider the following spreadsheet in Figure 2. This spreadsheet lists travel component costs for a single trip to New York. Each cost component is listed with a budget amount, an actual cost, and a category (optional or required). An item is ‘O’ if it is an optional item and ‘R’ if it is a required item.

Question #1: Write an Excel formula in cell E2 that can be copied down the column to determine if this item is within Budget.

The question requires comparing the actual cost of food ($250) with the budgeted value ($200). “Within budget” implies that the actual cost be less than or equal to the budgeted amount.

Since only two values are being compared, only a relational operator will be needed to implement this: $250<=$200. In Excel syntax this would be =D2<=C2.

Both operands D2 and C2 copy relatively.

Question #2: Write an Excel formula in cell E9 to determine (TRUE/FALSE) if all cost items are within budget.

When determining if all items meet specific criteria from a list, an And operation is required. It has already been determined in cells E2:E6 whether or not each specific cost item is within budget. If any one of these values is FALSE, the resulting value should be FALSE.

To implement an And operation, use the AND function. Using cells E2:E6, which already contain TRUE/FALSE values, an Excel formula can be written as follows: =AND(E2:E6).

Since this formula is not being copied, absolute cell referencing need not be considered.

What value should be displayed in cell E9 as a result of this formula? Substituting the Boolean values from cells E2:E6 into the function results in the formula =AND(FALSE, FALSE, TRUE, TRUE, TRUE). This reduces to the value FALSE since at least one argument is FALSE.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 86

123456789

10

A B C D E

ItemOptional/Required Budget

Trip to NY

Within Budget

Food R 200 250 FALSELodging R 350 500 FALSETours O 200 50 TRUESouvenirs O 100 75 TRUETransportation R 600 225 TRUE total 1450 1100 TRUE

All within budget FALSEAll Optional items within budget TRUE

Figure 2

Page 7: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

What not to do:

Using Ranges with Relational Operators:Instead of using the values solved for in question #1, could the formula be written to directly include the relational expressions? The answer is yes, but the format is very specific. If using relational expressions within an AND function, each individual comparison must be listed separately: =AND(D2<=C2,D3<=C3,D4<=C4,D5<=C5,D6<=C6). Clearly this is cumbersome and therefore not recommended.

Why not write the formula =AND(D2:D6<=C2:C6), using ranges on either side of the relational operator. This looks simple and easy to understand. However this is not a valid syntax and therefore will either result in a #Value! error or an incorrect result. Can you compare a range of values to a single value such as =AND(D2:D6>100)? No, this too is invalid.

These are common mistakes, but the reason why each is invalid syntax is clear: the meaning of such an expression is ambiguous, even to the user. For example, what would D2:D6<=C2:C6 mean? Would it mean that all values in D2:D6 are less than or equal to all values in C2:C6? Or the sum of the values in D2:D6 is less than or equal to the sum of the values in C2:C6? Would it mean that the value in D2 is less than or equal to the value in C2 and D3 is less than or equal to C3, etc?

One should think of relational operators the same way they think of arithmetic operators. Just as one would never write A2:A8+1, one should never write A2:A8>=1.

If a worksheet has a long list of items that need to be compared to a corresponding criterion to determine if they all meet their respective criterion, it is more efficient to first calculate a simple relational expression as in question 1, and then write an expression similar to the one in question 2. More advanced users can learn about working with arrays in Excel to perform these types of tasks.

Listing each cell reference individually:Another valid way to write this formula is =AND(E2,E3,E4,E5,E6). In cases with non-contiguous ranges this method works well. However for continuous ranges, it is recommended (just as in the case of SUM, MAX, MIN, etc.) that a range such as E2:E6 be used.

Question #3: Write an Excel formula in cell E10 to determine if all optional items are within budget.

Since both Tours and Souvenirs are optional they would both have to be within budget for this statement to be TRUE. To obtain a TRUE value if both individual items are TRUE, an And operation is required.

Using the AND function and the corresponding cell references for determining whether a cost is within in budget, the resulting formula is =And(E4,E5). Notice that the problem does not automatically determine which items are optional. The problem will require the writer to first check column B to

Relational Operators & Boolean Functions 1.7 CSE1111 Page 87

Page 8: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

determine if the cost category is optional (O), and then if it is, write the corresponding cell reference in column E which contains the TRUE/FALSE values for within budget.

As this formula is not copied, absolute cell referencing need not be considered.

Question #4: Write an Excel formula in cell E11 (not shown) to determine if the total cost of the NY trip is not more than $100 over budget and that the Tours component is the smallest component.

In this problem there are several sets of criteria which we need to be analyzed:

The trip is not more than $100 over budget. This can be represented mathematically as: actual value <= budget +100.

The tours component is the smallest component. This can be determined by finding the minimum value in a given range and then comparing it to the tours value: =Actual Tour value = minimum component value. If the Tours value is equal to the minimum value, the statement is TRUE.

Since both of the above criteria must be TRUE for our statement to be TRUE, an And operation will be needed.

In Excel syntax these can be represented as follows:

D7<=C7+100

D4=MIN(D2:D6)

Combining these two expression with an AND function results in the formula AND(D7<=C7+100,D4=MIN(D2:D6)). Will this formula result in a TRUE or a FALSE value?

Again since this formula is not copied, absolute referencing need not be considered.

Be careful when working with complex formulas and nested functions that the parentheses used exactly match: there must be an equal number of opening and closing parenthesis and they must be in the proper locations. Failure to do so can result in either error messages and/or invalid results.

THE OR FUNCTION SYNTAX

The OR function will evaluate a list of logical arguments to determine if at least one argument is TRUE. As with the AND function, arguments may consist of any combination of values, text strings, cell references or ranges such that each reduce to a TRUE or FALSE value. The syntax of the OR function is as follows:

Or(logical1, logical2,….).

Relational Operators & Boolean Functions 1.7 CSE1111 Page 88

Page 9: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

An Or function is only false if all arguments are false. Some Examples of using the OR function are listed in the following table:

Formula Value DescriptionOR(FALSE, FALSE, FALSE) FALSE The arguments include a list of Boolean

values directly entered as function arguments. Since all of the arguments are FALSE the final value is FALSE.

OR(25>24, 3<2+1) TRUE The arguments contain nested Relational expressions. As 25>24 is TRUE and 3<2+1 is FALSE the formula will be reduced to =OR(TRUE,FALSE) and then finally to TRUE.

OR(A1:A3) where cell A1 contains the value TRUE, and cells A2 & A3 contain the value FALSE

TRUE The argument listed is a range of cell references that contain TRUE/FALSE values. Since at least one argument is TRUE, the final value is TRUE.

OR(A1,A5<A4,MIN(A1:A5)=2) where cell A1 contains the value TRUE.

TRUE The 1st argument is a cell reference to a cell with a Boolean value, the 2nd contains a nested relational expression containing cell references, and the 3rd argument is a relational expression including a nested function. Since cell A1 contains the value TRUE, the result of the function is TRUE.

USING THE OR FUNCTION

Consider the spreadsheet seen in Figure 3. The spreadsheet is almost identical to the one from the previous example. Use this worksheet to answer the following questions.

Question #1: Write an Excel formula in cell E9 to determine (TRUE/FALSE) if at least one expense category is under budget. Assume the values in Column E are already provided.

This example is similar the one in question #2 in the AND function section. The difference here is that only one item need be within budget, not all, for a TRUE value to be returned. The expression “at least one” usually means an OR function will be required. The data to be evaluated is given in cells E3:E6.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 89

123456789

1011

A B C D E

ItemOptional/Required Budget

Trip to NY

Within Budget

Food R 200 250 FALSELodging R 350 500 FALSETours O 200 50 TRUESouvenirs O 100 75 TRUETransportation R 600 650 FALSE total 1450 1525 FALSE

At least one item within budget TRUEAny of the R items within budget FALSETake Trip FALSE

Figure 3

Page 10: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

Using the OR function the expression would be =OR(E2:E6).

This formula is not copied, so absolute cell referencing need not be considered.

If column E did not exist, each item would need to be evaluated separately: =Or(D2<=C2,D3<=C3,D4<=C4, D5<=C5, D6<=C6). Again, the syntax Or(D2:D6<=C2:C6) is not valid.

Question #2: Write an Excel formula in cell E10 to determine (TRUE/FALSE) if any of the required items (R) are within budget.

“Any” is also a keyword indicating the use of an OR function. In order to test only those items that are required (R), look in column B to determine the category and include only the cells in column E corresponding to cells containing ‘R’ values in column B. In this example rows 2, 3, and 6 contain required cost items.

In Excel syntax the resulting formula is =Or(E2,E3,E6). Since all three of these values are FALSE, the resulting value will be is FALSE. Again note that if any of the optional/required values changed, this formula would no longer be valid.

Again, the formula is not copied.

An automated way to solve this problem might be to use a separate column, say column J, to test whether or not each line item (food, lodging etc) is both required and within budget. For example, the formula in J2 would be =AND(B2=“R”,E2). This formula could then be copied down column J. The final step would be to use an OR function to determine if any of the resulting values in column J are TRUE: =OR(J2:J6).

Question #3: Write an Excel formula in cell E11 to determine (TRUE/FALSE) if this trip is a recommended. A trip is recommended if it is within the budget or if the transportation costs are less than $500.

To evaluate, first breakdown this problem into its components

Whether or not the total trip cost is within budget is already calculated in cell E7.

“Transportation cost is less than $500” can be written as the relational expression D6<500.

This expression should return a TRUE value if either of these two criteria is met. Either/or indicates the use of an OR operation.

To combine the criteria, write the formula =Or(E7, D6<500). This reduces to =Or(FALSE, FALSE) and results in a FALSE value.

Again, this formula is not copied.

THE NOT FUNCTION

Relational Operators & Boolean Functions 1.7 CSE1111 Page 90

Page 11: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

Consider the statement “The cost of food is not within budget for this trip.” Is this statement TRUE or FALSE based on the worksheet in Figure 4? Cell E2 tested whether or not the item was within budget. This is the exact opposite value of what is needed for ‘not within budget’. To answer this question the value in cell E2 just needs to be flipped from FALSE (it’s within budget), to TRUE (it’s not within budget). How can a TRUE value be changed to FALSE and vice versa?

THE NOT FUNCTION SYNTAX

The Not operation changes the Boolean value FALSE to TRUE and the Boolean value TRUE to false. In Excel, this corresponds to the NOT function. The NOT function will evaluate only one logical argument to determine if it is FALSE. If it FALSE, the function will return TRUE. Unlike the AND & OR functions the NOT function takes only one argument. The logical argument may consist of a single cell reference that contains a Boolean value or a relational expression or Boolean function that will reduce to TRUE or FALSE. The syntax of the NOT function is as follows:

Not(logical1)

Consider the following examples:

Formula Value DescriptionNOT(FALSE) TRUE The argument may be a Boolean value

directly entered into the function. Since the argument is FALSE the resulting value will be TRUE.

NOT (25>24) FALSE The argument may contain a nested Relational expression. As 25>24 is TRUE the value returned will be FALSE.

NOT(A1) where cell A1 contains the value FALSE

TRUE The argument may be a cell reference to a cell with a Boolean value. Since cell A1

Relational Operators & Boolean Functions 1.7 CSE1111 Page 91

12345678910

A B C D E F

ItemOptional/Required Budget

Trip to NY

Within Budget

Not Within Budget

Food R 200 250 FALSE TRUELodging R 350 500 FALSE TRUETours O 200 50 TRUE FALSESouvenirs O 100 75 TRUE FALSETransportation R 600 650 FALSE TRUE total 1450 1525 FALSE TRUE

None of the items are within budget FALSEOnly Optional items are within budget TRUE

Figure 4

Page 12: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

is FALSE the value returned will be TRUE.

NOT(MIN(A1:A5)=2) where cells A1 to A5 contain the value 3,4,5,6,7 respectively

TRUE The argument may include nested functions and relational expressions that reduce to a single TRUE or FALSE value. Since the MIN is not 2 – the formula will reduce to NOT(FALSE) and thus return TRUE.

Instead of writing the formula =NOT(E2), could the formula =D2>C2 be used? Yes, this is the logical equivalent. Frequently there are multiple ways of representing a logical construct, though this is not always the case. For example, to test if food is NOT a required expense (not ‘R’ in column B) the formula =NOT(B2=“R”) can be written. This is not the same as =B2=“O”, as there may be other categories other than “R” and “O” – perhaps an “L” category for luxury. If other categories existed then only the first formula would always result in the correct answer.

USING THE NOT FUNCTION – A SIMPLE EXAMPLE

Consider the travel expense worksheet (as seen in Figure 5) in the following example:

Write an Excel formula in cell F2 that can be copied down the column to determine whether this item is not within the budget. There are multiple ways to solve this problem; the following are a few of them:

Since we have already determined if an item is within budget, we simply want the opposite answer for not within budget. To change a TRUE to a FALSE or a FALSE to a TRUE, a NOT function can be used. The resulting formula would be = NOT(E2). This can then be copied down the column relatively.

If we did not already have a value in E2, we could also write =NOT(D2<=C2).

Still another way to solve this problem is to use a relational expression. Interpret the not within budget expression to mean greater than the budget, or =D2>C2. Again this can be copied down the column relatively.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 92

12345678910

A B C D E F

ItemOptional/Required Budget

Trip to NY

Within Budget

Not Within Budget

Food R 200 250 FALSE TRUELodging R 350 500 FALSE TRUETours O 200 50 TRUE FALSESouvenirs O 100 75 TRUE FALSETransportation R 600 650 FALSE TRUE total 1450 1525 FALSE TRUE

None of the items are within budget FALSEOnly Optional items are within budget TRUE

Figure 5

Page 13: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

USING THE NOT FUNCTION IN A “NONE OF” LOGICAL CONSTRUCT

Thus far we have been able to determine if a criterion is true for all items in a list, for at least one item in a list, or not true for a single item. How can it be determined if “none of items on a list meet a criterion?” A combination of logical operations can be used to accomplish this.

Step 1: Determine what is required to solve the problem logically:

Again using Figure 5, consider how to write an Excel formula in cell E9 to determine if none of the cost components are within budget. Logically this can be looked at in one of two ways:

A. If even one cost components is within budget, the statement that none of the costs components are within budget is FALSE. To find out if one value is TRUE in a list, use the OR function. Then, since a FALSE value need be returned if at least one value is TRUE, flip the result of the OR function with a NOT function.

=NOT(OR(food within budget, lodging within budget, tours within budget, etc.))

B. If all of the cost components are not within budget (i.e., all the values in column E are false), then the result of the formula should be TRUE. To determine if a value is FALSE, use the not function. Since the NOT function only evaluates one item at a time one would need to check each item separately. e.g., NOT(food is within budget), NOT(lodging is within budget), NOT(tours within budget), etc. Since all of these resulting values must be TRUE for the statement “none are within budget” to be TRUE, these individual NOT constructs can be combined with an AND function.

=AND(NOT(food is within budget), NOT(lodging is within budget), NOT(tours within budget), etc)

While the first method is logically equivalent to the second, whenever continuous lists are available, the first is much easier to write. Furthermore, in the first method, Excel would evaluate a maximum of two functions. Using the second method, Excel needs to evaluate 1+(the number of items in the list) functions resulting in a less efficient formula.

Step 2: Translate into Excel syntax:

Using the preferred method (method A), the Excel formula will be =NOT(OR(E2:E6)). Be careful to match parentheses exactly.

The alternative (not preferred) method is the formula:

=AND(NOT(E2),NOT(E3),NOT(E4),NOT(E5),NOT(E6))

Step 3: Consider if each reference should be absolute or relative:

Relational Operators & Boolean Functions 1.7 CSE1111 Page 93

Page 14: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

As this formula is not copied, the cell references should remain relative. If such a formula is copied, the same referencing rules that have been used previously apply within this construct.

What not to do:Do not write the expression =NOT(E2:E6). Since the NOT function only takes one logical value as an argument, this is incorrect syntax. To see why, think about what such a function would mean. Would it mean all of the values are false, or at least one value is false?

Relational Operators & Boolean Functions 1.7 CSE1111 Page 94

Page 15: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

EXERCISE 1.7-1 – RELATIONAL OPERATORS & BOOLEAN FUNCTIONS: STOCK ANALYSIS WORKSHEET

1. In cell E4 write a formula that can be copied down the column to determine if this company made a profit.

2. In cell F4 write a formula that can be copied down the column to determine the profit made by this company.

3. Write a formula in cell B10 to determine if all of the companies made a profit.

4. Write a formula in cell B11 to determine if at least one of the companies made a profit.

5. Write a formula in cell B12 to determine if ATT did not make a profit.

6. Write a formula in cell B13 to determine the maximum profit on any stock

7. Write a formula in cell B14 to determine if Microsoft made the maximum profit.

8. Write a formula in cell B15 to determine if Pepsi made less than the average profit.

9. Write a formula in cell B16 to determine if none of the stocks made a profit.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 95

12

345678910111213141516

A B C D E F

Stock Analysis Worksheet

Stock Price TodayPurchase Price #Shares

Made Profit

Calculate Profit

AT&T 43.25$ 41.00$ 100 TRUE 225.00$ Phillip Morris 108.00$ 76.00$ 75 TRUE 2,400.00$ Pepsi 33.40$ 43.75$ 200 FALSE (2,070.00)$ Miscrosoft 125.00$ 62.00$ 50 TRUE 3,150.00$ Subtotals 425

All shares made a profit FALSEAt least one stock made a profit TRUEATT did not make a profit FALSEMaximum Profit 3,150.00$ Microsoft made the Max profit TRUEPepsi made less than avg profit TRUENo shares made a profit FALSE

Page 16: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

EXERCISE 1.7-2 RELATIONAL OPERATORS & BOOLEAN FUNCTIONS: CONSTRUCTION TRADES JOB STAFFING

Your construction firm is currently staffing several new projects and as part of the planning phase you have set up a list of the trades that will be required for each job. For example, Job1 is a new requires only the skills of a stone mason and plumber, where Job2 requires the skills of a plumber, carpenter, and stone mason. Cells B2:F7 contain a TRUE value if the corresponding skill is required for that job. In the questions below, write the appropriate Excel formulas, which can be copied down the column (questions 1-6), for each of the following questions. (T/F – TRUE or FALSE).

1. T/F - This job uses all trades? (B10) ________________________________________

2. T/F - This job uses either carpenters or electricians? (C10) ________________________

3. T/F - This job does not require a plumber? (D10)_______________________________

4. T/F - This job uses none of the listed trades? (E10)______________________________

5. T/F - This job uses at least one trade? (F10) __________________________________

6. T/F – This job uses non-traditional construction requiring either a steel worker and/or a stone mason but not a carpenter? (G10-not shown)

______________________________________________________________7. Write an Excel formula that can be copied across the row to determine the

number of jobs in this category. (B16)

______________________________________________________________

Relational Operators & Boolean Functions 1.7 CSE1111 Page 96

12345678

910111213141516171819

A B C D E F

Plumber Carpenter Electrician Stone MasonSteel Worker

Job1 TRUE FALSE FALSE TRUE FALSEJob2 TRUE TRUE FALSE TRUE FALSEJob3 FALSE TRUE TRUE FALSE FALSEJob4 FALSE FALSE FALSE TRUE TRUEJob5 TRUE TRUE TRUE TRUE TRUEJob6 FALSE FALSE TRUE TRUE FALSE

All Trades

Either Carpenter or Electrician

Does not need Plumber

Needs none of the Trades

At least one Trade

Job1 FALSE FALSE FALSE FALSE TRUEJob2 FALSE TRUE FALSE FALSE TRUEJob3 FALSE TRUE TRUE FALSE TRUEJob4 FALSE FALSE TRUE FALSE TRUEJob5 TRUE TRUE FALSE FALSE TRUEJob6 FALSE TRUE TRUE FALSE TRUE #jobs 1 4 3 0 6

More people use all trades than no trades TRUE

Page 17: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

8. T/F - More jobs require all trades than no trades. (E18). This formula will not be

copied. ______________________________________________________

EXERCISE 1.7-3 RELATIONAL OPERATORS & BOOLEAN FUNCTIONS: AIRLINE PROBLEM

123456

A B C D E F G H I J K

Airline

Flies Columbus - NY, USA

Flies Columbus -Montreal, Canada

Flies Columbus -Chicago, USA

Flies Columbus - Los Angeles - USA

Has a frequent flyer program

Jane wants fly

Mark wants fly

Tom wants fly

Unaccept-able to All Total

Air Canada TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE 1America West TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE 2American FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE 0United TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE 2US Air TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE 2

You and your friends are trying to choose an airline to use for your summer travel plans. If all of you use the same airline the travel agent has agreed to give you 10% discount off each ticket. Each of you has different requirements for where you wish to go and each airline flies to different locations. Some of you also have requested that the airline have a frequent flyer program. Each person’s preferences are as follows:

Jane’s wants fly an airline that goes to all locations.

Mark’s wants to fly an airline that goes to NY or Chicago

Tom’s wants to fly an airline which does not go to a foreign country but does goes to NY

Answer the following questions using Excel formulas. Use cell references in your formulas wherever possible.1. Write a formula in G2, to be copied down, to determine if Jane wants to fly Air Canada.

______________________________________________________________

2. Write a formula in H2, to be copied down, to determine if Mark wants to fly Air Canada.

______________________________________________________________

3. Write a formula in I2, to be copied down, to determine if Tom wants to fly Air Canada.

______________________________________________________________

4. Write a formula in J2 (not shown) to test if this airline is unacceptable to everyone.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 97

Page 18: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

______________________________________________________________

5. Write a formula in K2 to be copied down to determine the number of people who want to fly Air Canada. _______________________________________________________

6. Write a formula in K7 (not shown) to determine (T/F) if more airlines are acceptable to Jane than to Mark.

______________________________________________________________

EXERCISE 1.7-4 RELATIONAL OPERATORS & BOOLEAN FUNCTIONS: CHAPTER REVIEW

You are the supervisor of an accounting department for a large corporation and need to evaluate your employees for possible promotions or if probationary action is required. Above is a spreadsheet that you have created to keep track of the name, salary, job level, retirement eligibility, performance review scores (1-worst to 5-best) for the past 3 years, and the year of last promotion for each employee. You will be completing this worksheet by filling in the required formulas.

1. Write an Excel formula to be put in cell I3 that can be copied down the column to calculate the average review score for the past 3 years of the employee. Round this value to the nearest hundredth.

2. Write an Excel formula in cell J3 that can be copied down the column to determine whether the employee’s average review exceeds 4.0. The result should be a TRUE/FALSE Boolean value.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 98

1

234567891011121314151617181920

A B C D E F G H I J K L

Employee Name Salary Job

LevelEligible for Retirement

2004 Review Score

2005 Review Score

2006 Review Score

Year of Last Promotion

Average Review Score

Average Review Score > 4.0

Promote Put on Probation

JENNIFER 45,600$ 1 FALSE 4 4 5 2006 4.33 TRUE FALSE FALSEIAN 45,600$ 1 FALSE 5 5 5 2005 5.00 TRUE TRUE FALSEBENJAMIN 56,000$ 2 FALSE 4 3 2 2004 3.00 FALSE FALSE FALSEALLAN 52,000$ 2 FALSE 3 4 3 2003 3.33 FALSE FALSE FALSEANDREA 63,300$ 3 FALSE 1 3 2 2003 2.00 FALSE FALSE FALSEANDREW 61,300$ 3 FALSE 5 1 4 2003 3.33 FALSE TRUE FALSEHENRY 61,300$ 3 FALSE 3 3 1 2006 2.33 FALSE FALSE TRUEBAEK 68,200$ 4 FALSE 2 3 3 2006 2.67 FALSE FALSE FALSEARTHUR 65,200$ 4 FALSE 3 4 2 2006 3.00 FALSE FALSE FALSESKYLER 72,500$ 5 TRUE 3 3 2 2006 2.67 FALSE FALSE FALSESONIA 74,530$ 5 FALSE 1 4 2 2004 2.33 FALSE FALSE FALSEJOSHUA 83,400$ 6 FALSE 1 2 3 2005 2.00 FALSE FALSE FALSEJONATHAN 78,600$ 6 TRUE 4 5 4 2004 4.33 TRUE TRUE FALSE

Quantity: 3 1

All employees with average scores >4.0 FALSENo one is eligible for retirement FALSETotal salary for current level 6 employees 162,000$

Employee Annual Review Analysis

Page 19: Chapter 7 - Spreadsheets: Relational & Boolean …web.cse.ohio-state.edu/cse1111/AUGUST 2013/Course Notes... · Web viewExercise 1.7-2 Relational Operators & Boolean Functions: Construction

Using Spreadsheets to Solve Problems

3. Write an Excel formula in cell F18 to determine whether (TRUE/FALSE) all the employees’ average review scores for the past 3 years exceed 4.0.

4. Write an Excel formula in cell K3 that can be copied down the column to determine whether (TRUE/FALSE) this employee is recommended for promotion this year. An employee will be recommended for promotion if they have a performance review score of over 4.0 in any of the past 3 years and have not been promoted after 2005.

5. Write an Excel formula in cell L3 that can be copied down the column to determine whether (TRUE/FALSE) this employee should be placed on probation. An employee should be placed on probation if their performance review score for 2006 is lower than 2.0 or if their average performance review score is lower than 2.0.

6. Write an Excel formula in cell F19 to determine (TRUE/FALSE) if there are no employees eligible for retirement.

7. Write an Excel formula in cell K16 that can be copied across to cell L16 to calculate the total number of employees that will be recommended for promotion or probation, respectively.

8. Write an Excel formula in cell F20 to calculate the total combined salaries of all employees on job level 6.

Relational Operators & Boolean Functions 1.7 CSE1111 Page 99