The IF Function - Computer Science and Engineering

advertisement
Chapter 4-IF Function and Boolean Functions
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
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. In this
Chapter 4 Page 1
Chapter 4-IF Function and Boolean Functions
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
Equal to
Not equal to
Greater than
Less than
Greater than or equal to
Less than or equal to
Operator Example
= 3+5=8
=
= Sum(3,7)<>10
<>
=100>Max(5,10,20)
>
= “B”<“A”
<
=B2=2 where B2
>=
contains the value 2
= 99<=8*3
<=
Resulting Value
TRUE
FALSE
TRUE
FALSE
TRUE
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.
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.

A
B
1200
1 total budget
2
3
4
5
6
7
8
9
C
D
New
Boston >
York
Boston NY
250
110 FALSE
300
189 FALSE
660
776 TRUE
1210
1075 FALSE
Item
Food
Lodging
Travel
total
Within budget
Travel the largest cost
10 component
FALSE TRUE
TRUE
TRUE
Compare the corresponding two values to get a
Figure 1
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.
Chapter 4 Page 2
Chapter 4-IF Function and Boolean Functions

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.
=B7<=$B1 would be required.
If the range name were not used, the formula
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.

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).
Chapter 4 Page 3
Chapter 4-IF Function and Boolean Functions

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:

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
Chapter 4 Page 4
Chapter 4-IF Function and Boolean Functions
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
AND(TRUE, TRUE, TRUE)
Value
TRUE
AND(25>24, 3<=2+1)
TRUE
AND(A1:A3)
where cell
A1 FALSE
contains the value FALSE, and cells
A2 & A3 contain the value TRUE
AND(A1,A5<A4,MIN(A1:A5)=2) FALSE
where cell A1 contains the value
FALSE
Description
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.
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.
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.
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
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.
A
1
2
3
4
5
6
7
8
9
10
B
C
Optional/
Required
Item
Food
R
Lodging
R
Tours
O
Souvenirs
O
Transportation
R
total
Budget
200
350
200
100
600
1450
D
Trip to
NY
250
500
50
75
225
1100
All within budget
All Optional items within budget
E
Within
Budget
FALSE
FALSE
TRUE
TRUE
TRUE
TRUE
FALSE
TRUE
Figure 2

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.
Chapter 4 Page 5
Chapter 4-IF Function and Boolean Functions
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.
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 noncontiguous 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.
Chapter 4 Page 6
Chapter 4-IF Function and Boolean Functions
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 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.
Chapter 4 Page 7
Chapter 4-IF Function and Boolean Functions
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,….).
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
OR(FALSE, FALSE, FALSE)
Value
FALSE
OR(25>24, 3<2+1)
TRUE
OR(A1:A3) where cell A1 contains TRUE
the value TRUE, and cells A2 & A3
contain the value FALSE
OR(A1,A5<A4,MIN(A1:A5)=2) TRUE
where cell A1 contains the value
TRUE.
Description
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.
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.
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.
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
A
1
2
3
4
5
6
7
8
9
10
11
B
C
Optional/
Required
Item
Food
R
Lodging
R
Tours
O
Souvenirs
O
Transportation
R
total
Budget
200
350
200
100
600
1450
At least one item within budget
Any of the R items within budget
Take Trip
Figure 3
D
E
Trip to
Within
NY
Budget
250 FALSE
500 FALSE
50 TRUE
75 TRUE
650 FALSE
1525 FALSE
TRUE
FALSE
FALSE
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.
Chapter 4 Page 8
Chapter 4-IF Function and Boolean Functions
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.

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.
Chapter 4 Page 9
Chapter 4-IF Function and Boolean Functions
THE NOT FUNCTION
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
A
1
2
3
4
5
6
7
8
9
10
Item
Food
Lodging
Tours
Souvenirs
Transportation
total
B
Optional/
Required
R
R
O
O
R
C
Budget
200
350
200
100
600
1450
None of the items are within budget
Only Optional items are within budget
D
Trip to
NY
250
500
50
75
650
1525
E
Within
Budget
FALSE
FALSE
TRUE
TRUE
FALSE
FALSE
F
Not
Within
Budget
TRUE
TRUE
FALSE
FALSE
TRUE
TRUE
FALSE
TRUE
Figure 4
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
NOT(FALSE)
TRUE
Description
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 TRUE The argument may be a cell reference to a cell with a
contains the value FALSE
Boolean value. Since cell A1 is FALSE the value
returned will be TRUE.
NOT(MIN(A1:A5)=2)
TRUE The argument may include nested functions and
where cells A1 to A5 contain
relational expressions that reduce to a single TRUE or
the
value
3,4,5,6,7
FALSE value. Since the MIN is not 2 – the formula will
respectively
reduce to NOT(FALSE) and thus return TRUE.
Chapter 4 Page 10
Chapter 4-IF Function and Boolean Functions
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.
A
1
2
3
4
5
6
7
8
9
10
Item
Food
Lodging
Tours
Souvenirs
Transportation
total
B
Optional/
Required
R
R
O
O
R
C
Budget
200
350
200
100
600
1450
None of the items are within budget
Only Optional items are within budget
D
Trip to
NY
250
500
50
75
650
1525
E
Within
Budget
FALSE
FALSE
TRUE
TRUE
FALSE
FALSE
F
Not
Within
Budget
TRUE
TRUE
FALSE
FALSE
TRUE
TRUE
FALSE
TRUE
Figure 5
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:
Chapter 4 Page 11
Chapter 4-IF Function and Boolean Functions
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:
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?
Chapter 4 Page 12
Chapter 4-IF Function and Boolean Functions
The IF Function
With relational operators and Boolean logical functions we have been able to determine if data
meets a set of criteria by returning the Boolean Logical values TRUE or FALSE. What if instead
of determining if a value is TRUE or FALSE, we wish to type a status message (e.g., “Yes” or
“No”) or perform one calculation versus another? The Excel IF function has the ability to
logically analyze information, and the flexibility to return answers other than TRUE and
FALSE.
SIMPLE IF FUNCTIONS
THE IF FUNCTION SYNTAX
The logical IF function takes a Boolean value as its first argument. This can either be the result
of a Boolean function, relational expression, or a reference to a cell containing a Boolean value.
The function tests whether the value is TRUE or FALSE. If the value is TRUE, the computer
performs the action specified by its second argument. If the comparison is FALSE the computer
performs the action specified by its third argument. The syntax of an IF function is:
IF (logical_test, value_if_true, value_ if_ false)
The use of the IF function corresponds to the use of the word “if” in everyday English. In the
sentence “if it is raining today, I will stay inside; otherwise, I will go outside” the logical test is
whether or not it is raining, the value if true is staying inside, and the value if false is going
outside.
As an example, consider the spreadsheet in Figure 1. The spreadsheet lists the names of several
employees, their monthly wage (column C), and their benefit eligibility (column B). Since it is
not clear what a TRUE or FALSE value in column B indicates (i.e., does the TRUE in cell C4
mean John is eligible for benefits, or is it TRUE that he is he is ineligible for benefits?), it is
desirable to list whether or not an employee is eligible for benefits in a more user-friendly way.
One of the simplest examples of the use of the IF function is to take a TRUE or FALSE value
and return an associated text label.
A
1 Medical Premium
2
3
4
5
6
Name
Erlich, John
Carson, Betty
Thomas, Justin
B
$
C
D
E
F
Medical
Premium
300
0
300
Total
Compensation
$
1,800
$
750
$
2,500
300
Benefit
Eligibility
TRUE
FALSE
TRUE
Benefit
Status
Wage
1500
eligible
750 ineligible
2200
eligible
Figure 6
Chapter 4 Page 13
Chapter 4-IF Function and Boolean Functions
Assume that the TRUE value in column B indicates that an employee is eligible for benefits. In
column D the IF function can be used to display the text “eligible” or “ineligible” based on the
value in column B. To use the IF function in cell D4, consider each of the three arguments: the
logical test, the value if true, and the value if false.



The first argument is the logical test. The logical test is the value that the decision of the
IF function is based on. Whatever is placed in this argument must ultimately reduce to a
single TRUE or FALSE value. The formula being written needs to determine whether or
not John is eligible for benefits. This is indicated by a Boolean value in cell B, so the
logical test will be to reference the value in cell B4.
The second argument is the value to be displayed if the logical test is evaluated to be
TRUE; in this case it will be the text label “eligible”.
The third argument is the value to be displayed if the logical test is evaluated to be
FALSE; in this case it will be the text label “ineligible”.
The resulting formula will then be:
=IF(B4,“eligible”,“ineligible”)
Notice that the logical test simply references the cell, since the cell already has a TRUE/FALSE
value. It is not necessary to write B4=TRUE for this argument. Also note the value_if_true and
the value_if_false arguments are enclosed in quotes. This is because the resulting values to be
displayed are text. If the quotes are omitted the computer will look for a range named eligible
and either returns the value from that range or gives a #NAME! error if the range name does
not exist.
A SIMPLE EXAMPLE
While displaying whether or not an employee is eligible for benefits is nice, frequently
translating a Boolean value to a text label is not enough. What if we needed to calculate values,
not just display text, based on whether or not certain criteria are met? One example might be
calculating an employee’s total compensation including medical benefits, where the company
pays the premium for benefit eligible employees. This would require $300 be added to the
salary if the employee was eligible for the benefit.
To begin to answer this question, consider the simpler problem of determining the benefit
value each employee should receive: What formula will be needed in cell D4 that can be copied
down the column to determine medical benefit value (cost of premium) for this employee? The
benefit value is $300 if the employee is eligible for benefits (as listed in cell B1).

To solve this problem, first determine if John receives benefits by looking in the
corresponding cell in column B. The TRUE value indicates that he does receive benefits
(logical test). Based on the problem statement, if he receives benefits the medical premium
is $300 (result if TRUE). If he does not receive benefits there is no premium. i.e., the
premium is $0 (result if FALSE).
Chapter 4 Page 14
Chapter 4-IF Function and Boolean Functions

To decide between different results based on some criterion, use an IF function. To better
understand the logical structure of this problem, consider writing out the IF function with
English text without values inserted (this is called “pseudo code”):
If(eligible for benefits, $300, otherwise $0).
Translated into Excel syntax this would be: =If(B4,B1,0).

Now consider whether each cell reference should be relative or absolute. The formula
needed in cell D5 is =IF(B5,B1,0). The value needed for the logical_test changes relatively
(B4 to B5). However, the benefit amount remains $300 (as given in cell B1), so this
reference should have an absolute row. The value if there is no benefit is just the constant
$0, which does not change. The final formula is:
=If(B4,B$1,0).
To calculate total compensation, the medical premium value can be added to the monthly
salary with the formula =C4+E4. A somewhat more complex question would be to calculate
this total compensation directly, without first calculating the premium value in a separate cell.
To do this, the formula =C4+IF(B4,B$1,0) could be used. Another possible way of writing
this formula is =IF(B4,B$1+C4,C4), placing the addition inside of the IF statement for both
the values_if_true and the value_if_false arguments.
What not to do:
What would happen if we instead wrote the formula =If(B4=TRUE,B1,0)? This syntax works
equally as well. Frequently however, students try to use quotes around “TRUE” resulting the
formula. =If(B4=“TRUE”,B1,0). “TRUE” with quotes is a label, while TRUE without quotes is
a value. As with numbers, a label used instead of a value (such as “FALSE” instead of FALSE)
can lead to incorrect results. Be careful never to use quotes with Boolean values.
Furthermore, the expressions B4=TRUE and B4 are logically equivalent if B4 contains a
Boolean value. To see why, consider the possible values of cell B4. If cell B4 contains TRUE,
then the result of the relational expression B4=TRUE is true. In this case, the expression B4
is TRUE as well. Now, if B4 contains FALSE, the result of the relational expression B4=TRUE
is FALSE. The result of the expression B4 is FALSE in this case as well. In either case (B4
contains TRUE or contains FALSE), the two expressions are equivalent. Therefore it is not
necessary to write B4=TRUE.
NESTING RELATIONAL EXPRESSIONS FOR A LOGICAL TEST
Let’s change the original example to the spreadsheet seen in Figure 2. Now assume the
employer will pay 50% of the medical premium for employees who qualify for medical benefits
and to qualify for benefits, employees must earn at least $1000 per month. Write an Excel
formula in cell C4 that can be copied down the column to determine the company’s
contribution to the medical premium of each employee.
Chapter 4 Page 15
Chapter 4-IF Function and Boolean Functions
A
1 Medical Premium
2 % Paid by company
3
4
5
6
Name
Erlich, John
Carson, Betty
Thomas, Justin
B
C
300
50% Medical
Premium by
Company
Wage
1500
150
750
0
2200
150
D
$
Total
Compensation
$
1,650
$
750
$
2,350
Figure 7


Logically, first determine if John receives benefits by comparing his wage to the $1000
minimum set by the company. Does John receive this $1000 minimum? If John does
qualify, calculate the medical premium paid by the company as 50% of $300. If John does
not qualify the benefit is $0.
To evaluate a condition and decide between different solutions, use the IF function. In
pseudo code: If(a person qualifies for benefits by earning more than $1000, then company
pays 50% of the premium, otherwise $0)
o Notice that there are no TRUE/FALSE values already listed on the worksheet that
describe whether or not an employee qualified for benefits. This calculation will need
to be nested within the IF function’s logical_test. To compare two values in Excel
(John’s salary vs. $1000), a relational expression can be used. The relational
expression B4>=1000 will evaluate to the TRUE/FALSE value needed for the logical
test argument.
o Also note that the value if true, 50% of $300, is also not listed. Again the expression
will need to be nested within the value_if_true argument, in this case B1*B2.
Translated into Excel syntax the formula is =IF(B4>=1000,B1*B2,0).

Is the formula copied? Yes. To determine if any modifications are needed, consider how
each of the values changes when the formula is copied down the column. Does the monthly
salary change from B4 to B5? Yes. Does B1 become B2? No, the value of the benefits is
fixed. Similarly the value of the percentage paid the company in cell B2 is fixed. Hence the
final version of the formula should be =If(B4>=1000,B$1*B$2,0)
NESTING BOOLEAN LOGICAL FUNCTIONS FOR A LOGICAL TEST
What if this problem were again changed to require an employee to have a salary between
$1000 and $2000 (inclusive) in order to qualify for medical benefits? Changing the criteria
will require the logical test (first argument) of the IF function to be modified accordingly.
Here both the criteria, that a value is greater than or equal to 1000 and that the value is less
than or equal to 2000, need to be true for the statement to be true. Since both criteria must
be true for the statement to be true, an AND function will be needed. If solving just for this
criterion the following expression could be used: AND(B4>=1000,B4<=2000). Nesting this
within the formula results in the following:
=If(And(B4>=1000,B4<=2000),B$1*B$2,0)
Chapter 4 Page 16
Chapter 4-IF Function and Boolean Functions
NESTING IF FUNCTIONS
Thus far we have used IF functions to evaluate a single logical test and decide between one of
two outcomes based on that test. These outcomes may be inserting text into the spreadsheet,
displaying a value, or even the result of a nested formula. Frequently problems arise that have
more than two outcomes. For example, a ticket for a movie is one price for children, another for
adults, and a third for senior citizens. How can that type of problem be solved in Excel?
We have already seen how nesting can be used to test for complex criteria or to calculate values
where arguments of functions are themselves nested formulas/functions. Nesting can also be
used with an IF function to decide between more than two different results. What if the
calculation in the value_if_false argument contained another IF function? If the computer
reaches this point, because the logical test in the first IF is FALSE, the computer will calculate
the value of this 2nd IF and use the result as the FALSE (3rd argument) of the 1st IF. Let’s take a
deeper look into how this might work.
A PROBLEM WITH 3 POSSIBLE OUTCOMES:
Again consider the value of an employee’s medical benefits, but this time the criteria will be
that the employer will pay no benefits to those earning less than $1000, 50% of the premium
for those earning at least $1000 but less than $2000, and the full premium for those receiving
wages of $2000 or more. In order to decide the value of the company’s benefit contribution it
will be necessary to first determine which criterion applies. To understand this problem a little
better (step 1), a decision tree has been created in Figure 3 which visually represents the logic
in this problem.
Company
Contribution $0
TRUE
Does
this
person
earn
less
than
Company
Contribution
FALSE
Does this
person earn
at least $1000
but less than
$2000
TRUE
FALSE
50%
Company
Contribution
100%
Figure 8
A diamond represents a decision with two possible outcomes: TRUE or FALSE. Each outcome
is either a specific value, such as $0, or leads to another diamond where the next decision is
made. In Excel, each of the diamonds can be implemented using an IF function, so the
value_if_true and/or the value_if_false can simply be another IF function. Up to 7 levels of
nesting can be used in this manner.
Chapter 4 Page 17
Chapter 4-IF Function and Boolean Functions
In this decision tree the first diamond tests to see if John’s wages are less than $1000. If they
are then $0 is returned. If John’s wages are not less than $1000 the next diamond will test
whether or not John’s wages are at least $1000 but less than $2000. If this is TRUE then a
calculation of 50% of the premium cost will be returned; otherwise the entire cost of the
premium will be returned.
This will be translated into Excel syntax (step 2) based on the worksheet in Figure 4.
A
1 Medical Premium
2
3
4
5
6
Name
Erlich, John
Carson, Betty
Thomas, Justin
B
$
C
D
E
300
Medical
Premium paid
Wage
by company
1500
150
750
0
2200
300
Total
Compensation
$
1,650
$
750
$
2,500
Category
Management
Hourly
Hourly
Figure 9
Given the complexity, let’s first translate the decision tree into Excel psuedo code using an IF
function:
If(John’s wage<1000, $0 for premium, otherwise if (John’s wage is at least $1000 but
less than $2000, 50% premium, otherwise 100% premium)).
Next, translate this into the proper Excel syntax. Be sure the formula contains an identical
number of openning and closing parentheses in the correct places:
=If(B4<1000,0,If(B4<2000,B1*0.5,B1))
Finally, for step 3 add relative/absolute cell referencing to copy this formula down the column:
=IF(B4<1000,0,IF(B4<2000,B$1*0.5,B$1)).
This can also be written as IF(B4<1000,0,If(B4<2000,0.5,1))* B$1.
Look at the second IF logical test. One may ask why the logical_test doesn’t evaluate that both
John’s wage is at least $1000 and that his wage is less than $2000 (i.e., AND(B4>=1000,
B4<2000)). To see why it is okay to assume that B4 is greater than or equal to 1000, consider
how the function works: First the computer will evaluate the first IF, testing to see if B4<1000.
If it is less (TRUE) then the computer will return the answer $0 and the calculation is complete.
If the logical test is FALSE (B4 is not less than $1000) Excel will continue evaluating the
expression by going to the second IF statement. Thus the value could not possibly be less than
$1000 if the second IF is evaluated, so it need not be tested.
In some problems, it will matter which case is analyzed first. In this problem each category is
mutually exclusive. A wage can only fall into one category (either <$1000, >=$2000 or in
between). Therefore the order of the nesting is not important. The formula could have just as
easily been written as follows: =IF(B4>=2000,B$1,IF(B4<1000,0,0.5*B$1)). One could
Chapter 4 Page 18
Chapter 4-IF Function and Boolean Functions
also test the “between 1000 and 2000” criteria first, although this would require the more
complex logical test using a Boolean function and is not recommended:
=IF(And(B4>=1000,B4<2000), 0.5*B$1, IF(B4<1000,0,B$1)).
A case where the order would matter is if we set the criteria for the company’s contribution to
medical premiums as follows:


The company does not pay benefits for management (column E – Category).
The company will pay full premiums for hourly employees who earn over $1000 and
50% for those who earn $1000 or less.
Here we do not have mutually exclusive categories. A person can earn over $1000 and still not
qualify, as in the case of management. So the first logical test must be to determine which
employment category an employee is in, and only if they are not in management apply the
second set of criteria based on wages. This formula can be written as follows:
= IF(E4= “management”,0,IF(B4<=1000,.5*B$1,B$1))
Chapter 4 Page 19
Chapter 4-IF Function and Boolean Functions
Practice Problem Chapter 4.1 Simple If’s – Buying a USB drive
A
B
C
D
1
2
Simple If's - USB Flash Drive Analysis
3
4
Hurdle Amount:
5
6
7
8
9
10
11
12
13
Item
Sandisk Cruzer Micro
A-DATA PD10 Rotating Lid
PQI Cool Drive U339 USB 2.0
PQI Cool Drive U339 4GB USB 2.0
Apacer Handy Steno AH320
Sandisk Cruzer Micro U3 4GB
Kingston DataTraveler
ACP-EP Memory 2 GB USB 2.0
Price
$ 29.99
$ 10.99
$ 12.99
$ 32.99
$ 10.99
$ 49.99
$ 34.00
$ 20.00
Less then
hurdle price
TRUE
TRUE
TRUE
FALSE
TRUE
FALSE
FALSE
TRUE
$
E
F
Amount
Spent
$
29.99
$
10.99
$
12.99
$
$
10.99
$
$
$
20.00
Alernative
Purchase Criteria
$
59.98
$
32.97
$
38.97
$
65.98
$
32.97
$
$
68.00
$
40.00
30.00
Buy or Do
not Buy
buy
buy
buy
do not buy
buy
do not buy
do not buy
buy
1. Write an Excel formula in cell C6, which can be copied down, to determine if this USB drive
is less than the maximum amount (hurdle) you are willing to spend on a USB drive.
2. Write an Excel formula in cell D6 that indicates whether or not to buy this USB drive. The
formula should return the text “buy” if you wish to purchase it, and “do not buy” if you do
not.
3. Write an Excel formula in cell E6 to determine how much money you would spend, if any, on
this USB drive. You will buy one drive if it costs less than the hurdle amount. Make sure you
can copy it down the column.
4. Alternatively, consider a different buying (and budget) plan as follows:



If the cost of the USB drive is less than $20, then you will purchase 3 drives
If the cost of the USB drive is at least $20 but less than or equal to $35, then you will
purchase 2 drives
If the cost of the USB drive is over $35 then you purchase no USB drives.
Write an Excel formula in cell F6, which can be copied down the column, to determine how
much money you will spend, if any, on the purchase of these USB drives using this new buying
plan.
Chapter 4 Page 20
Chapter 4-IF Function and Boolean Functions
Practice Problem-Chapter 4.2 Using Nested If Functions
1
2
3
4
5
6
7
8
A
Name
Smith
Jones
Doe
B
Total Points
1000
2455
645
0
1000
2000
C
B
A
C
Grade
1. Write an EXCEL formula in cell C2 to determine the grade received by Smith .
Write it so that it can be copied for the other students. Use only cell addresses. Do not
use any AND, OR or NOT functions. The grades are based on the following criteria:
If you received 2000 you get an A, if you received between 1000 and 2000
you get a B, if you received less than 1000 you get a C.
1
2
3
4
5
A
Name
Charles
Elliot
Sam
Jane
B
English Grade
55
68
81
95
C
Science Grade
72
86
50
82
2. Determine which scholarship/if any Jane has won:
If Jane has the highest grade in English and Science then indicate she “won #1.”, if
she doesn’t qualify for scholarship #1 but her average of Science and English is
greater than 85 then indicate she “won #2”, if she hasn’t won either scholarship
return the response “sorry”.
3. Write a nested if to determine the name of the student with the highest English
grade.
Chapter 4 Page 21
Chapter 4-IF Function and Boolean Functions
Practice Problem-Chapter 4.3 BB’s All-night Convenience Store
A
1
2
3
4
5
6
7
B
C
D
Sales Taxes
Liquor
Food
Other
12.0%
0%
5.75%
You have been hired as a consultant to help
computerize BB’s record keeping system in an effort to
improve stores profits. You have created a partial list
of inventory but before entering the rest of the data
you’d like to get started writing the formulas you will
need for the analysis. You know that if you write the
formulas correctly it will be easy to insert new rows of
data and copy the existing formulas as needed. You
will also be careful that the totals you create will
update automatically.
Income Taxes
0
5.0%
25,000.00
60,000.00
15.0%
20.0%
150,000.00
22.5%
Taxes
A
B
C
D
E
F
G
H
I
J
K
reorder
Final Price
with
SalesTax
reorder
amount
ordering
category
annual profit
BB's All Night Convenience Store
1
2
Item
category
Retail
Selling
Price
3
4
Absolut Vodka
Liquor
$19.75
$9.75
10
20
TRUE
$22.12
25
high
Coca Cola
Stolinakya
Vodka
Food
$1.39
$0.89
135
52
FALSE
$1.39
0
low
Liquor
$16.00
$4.00
300
260
TRUE
$17.92
260
high
Napkins
Other
$2.99
$1.14
29
120
TRUE
$3.16
120
medium
Pen
Jack Daniels
Whiskey
Other
$0.99
$0.66
234
15
FALSE
$1.05
0
low
Liquor
$15.75
$8.75
103
59
FALSE
$17.64
0
low
Food
$2.99
$1.29
22
123
TRUE
$2.99
123
medium
$26,845.00
$8,250.84
Liquor
$16.25
$9.25
88
29
FALSE
$18.20
0
low
$13,949.00
5
6
7
8
9
Potato Chips
Jim Beam
Bourbon
10
11
12 Totals
Profit/
Item
Items On
Hand
Avg. Units
Sold Per
Avg Week
4
$10,140.00
$2,406.56
$54,080.00
$7,113.60
$514.80
$78,299.80
Calc
1. Ideally BB’s likes to keep an inventory of at least 1 ½ weeks of product on hand (1.5 x
average units sold per week). Write and Excel formula in cell Calc!G3, which can be copied
down, to determine if this item needs to be reordered.
2. According to state rules, liquor is taxed at 12%, food has no tax and all other items are taxed
at 5.75%. Write an Excel formula in cell Calc!H3, which can be copied down, to determine
the final price of this item including tax. As sales taxes are subject to change, be sure to use
only cell references in your formula.
Chapter 4 Page 22
Chapter 4-IF Function and Boolean Functions
3. Write an Excel formula in cell Calc!I3, which can be copied down the column, to determine
the reorder quantity for this item. (Challenge – try to solve this using w/o nesting IF’s)
 If you have determined this item should be reordered, then you will reorder one
week’s worth of product, unless this amount is less than the minimum
order quantity of 25 units then you will order 25 units.
 Items not being reordered should display the quantity 0.
4. BB’s is fairly busy and wants to prioritize their ordering activities. To do this he will
categorize each product order as high, medium or low priority. Write an Excel
formula in cell Calc!J3, which can be copied down the column to determine the order
category of this item based on the following criteria:
 If the item is liquor and needs to be reordered it is “high” priority
 If the item needs to be reordered and is either food or has a reorder quantity of over 100
the it’s a “medium” priority
 Otherwise its low priority
5. Write an Excel formula in cell Calc!K3, which can be copied down the column, to determine
expected annual profit from this item assuming 52 weeks of sales, the profit per item and
average quantity sold per week.
6. Write an Excel formula in cell Calc!K12 to determine the total expected annual profits.
Total expected annual profits are defined as expected profits for all items minus overhead
and salary expenses. Overhead and salary expenses are estimated at $45,000 per year.
7. Write an Excel formula in cell Calc!K13 (not shown) to determine income tax liability owed
based on the income tax table given on sheet Taxes!. According to that table if profits are
less than $25,000 then 5% of the total profit is owed. If profits are at least $25,000 but less
than $60,000 then 15% of the total profit is owed, etc.
Chapter 4 Page 23
Download