Functions Making Decisions in Spreadsheets

Robert P. Webber, Scott McElfresh, Don Blaheta, Longwood University


Why are we working on this? Many interesting algorithms require different computations depending on the values of (some of) the data. Any complete language for specifying algorithms, including spreadsheets' expression systems, needs a way to express conditional execution. Skills in this section: Express conditional computation using IF — Combine conditions using AND, OR, NOT Concepts: Algorithm representation

Spreadsheets have functions that allow varying calculations to happen. For instance, in January, you are unlikely to want to calculate the heat index, but you may wish to calculate a wind chill. Most computer programming, including spreadsheets relies on something called a Boolean expression, named after a mathematician named George Boole. Technically, a Boolean expression is something that evaluates to either TRUE or FALSE. We might view it as an answer to a yes-or-no question, with no ambiguities. views it as a judgement about a particular statement.

Generally, when involving decisions, computer programming involves separating the possibilities into two paths, and a question that can be asked to determine which path to take.

For instance, someone may say "I am going to go buy potato chips. If Walmart has bags over 72 ounces, I will buy them there. If not, I will go buy chips at Food Lion." There is a clear situation that will determine this person's course of action. Somehow they need to ascertain whether Walmart sells bags of chips over 72 ounces. He/she may do this by calling and asking, or going there and finding out themselves, or asking a friend who has been there recently. In any case, this person will then know whether to buy at Walmart or at Food Lion.

The question being asked must have a clear answer. For instance, in English, we might say "If it is supposed to rain today, I will take an umbrella." Often implied with this is that the person will NOT take an umbrella if the forecast calls for sun. The "question" in this case: "Is it supposed to rain today?" We can answer this question by looking online for a weather forecast. However, if the forecast calls for a 50% chance of rain, what do you do? It must be assumed that there is a threshhold at which the person says "Yes, it is supposed to rain today." In the computer, there is no room for uncertainty. It is either true or false.

So what are the questions we can ask about values in spreadsheets? The most important ones are equality and inequality; they are defined as you learned them in math class, although the limitations of the keyboard means we sometimes write them a little differently:

Comparison Math symbol Spreadsheet operator
Less than < <
Greater than > >
Less than or equal to <=
Greater than or equal to >=
Equal to = =
Not equal to <>

If you use one of the spreadsheet operators in a cell's expression, the spreadsheet will display the boolean result. For instance, if you put =A4>=7 in a cell, that cell would show TRUE whenever the value in cell A4 is a number 7 or greater, and FALSE otherwise.

In addition, there are a number of functions whose computed result is boolean. Some of the more important to us are:

ISNUMBER(...) This function evaluates as TRUE if the argument is a number, and FALSE otherwise. For instance, ISNUMBER(A1) is TRUE if cell A1 contains the number 14, say, and false if it contains the word "fourteen."

ISBLANK(...) This function evaluates as TRUE if its argument is a blank cell, and FALSE otherwise. Note that a cell is not considered truly "blank" if it contains anything; if you go to (say) cell C2 and press the spacebar, then a cell with the expression =ISBLANK(C2) would now say FALSE.

Conditional computation with IF

IF(...) The IF function tests a condition and returns one of two values, depending on the result of the test. The condition must evaluate to true or false. The IF function takes three arguments. Its general form is

IF(condition, value_if_true, value_if_false)

If the condition is true, the function evaluates the first value. If the condition is false, it evaluates the second value.

Side note: The third argument may be omitted. If so, the statement returns FALSE if the condition is false.

A sample use of IF() is to prevent division by zero. Consider the expression

=A3 / A4

If cell A4 contains 0, the expression results in a error, because division by 0 is undefined. Instead, write

=IF (A4 <> 0, A3 / A4, "Error")

Now, if A4 does not contain 0, the division is done. If it is 0, the division is skipped, and the word Error is displayed.

Example: Consider the scenario of a vendor near an performance stage. Below are 4 upcoming shows with the number of tickets sold and the length (in minutes) of each show.

1 Tickets Show length
2 500 60
3 650 75
4 425 50
5 300 80

Past experience shows that if a show is longer than an hour, the vendor will sell an average of $5 of food per ticket sold, but if the show is not longer than an hour, the vendor will only sell an average of $3 of food per ticket. We can have the spreadsheet help us figure out how much income to predict.

In Cell C2, type = IF( B2 > 60, 5*A2, 3*A2) and fill down to cell C5. The result:

1 Tickets Show length Anticipated income
2 500 601500
3 650 753250
4 425 501275
5 300 801500

Example: Now suppose a spreadsheet contains a list of employees and their annual salaries. Employee names are in column A, last year's salaries in column C, and this year's salaries in column D. However, some of the employees are new, and their old salary fields are blank. Here is the data.

1Name Old salary New salary
3Aaron, Ann 25000 26200
4Bolding, Bill 30000
5Brown, Buddy 45100 46000
6Dodge, Jane 36000
7Emerson, Kim 38200 39200
8Etheridge, Alice 40000 40000
9Falconer, Faye 32000 34500
10Godwyn, Gregory 41000 42000

The problem is to calculate the percent of increase in salary for the continuing employees and put the results in column E. Leave the cell in column E blank if the employee is new.

To calculate the percent of increase,

For example, consider Ann Aaron's figures.

Increase = 26200 – 25000 = 1200 ,

Proportion of increase = 1200 / 25000 = 0.048 ,

Percent of increase = 0.048 * 100 = 4.8 .

We can combine these into one line.

Percent of increase = (25200 – 25000) / 25000 * 100

Using cell references, Ann's percent of increase is (D3-C3) / C3 * 100 . However, we should do this calculation only if cell C3 (the old salary) actually contains a number.

So we guard the calculation with an IF(). Put ISNUMBER(C3) as the condition, the expression for calculating the percent increase in the second argument, and "" in the third. If C3 contains a number, the calculation will be done, and the result displayed. Otherwise, a blank will be displayed. That is, put the following expression in cell E3.

=IF( ISNUMBER(C3), (D3-C3)/C3*100, " ")

Then click and drag this expression down column E. Here are the results.

1Name Old salary New salary Pct increase
3Aaron, Ann 25000 26200 4.8
4Bolding, Bill 30000
5Brown, Buddy 45100 46000 1.9956
6Dodge, Jane 36000
7Emerson, Kim 38200 39200 2.6178
8Etheridge, Alice 40000 40000 0
9Falconer, Faye 32000 34500 7.8125
10Godwyn, Gregory 41000 42000 2.4390

More complicated questions

There are times when the situation calls for a more complicated question. Some examples:
"I will be very happy tonight only if the cafeteria has brownies, and my favorite act wins America's Got Talent".
In this case, there are 2 situations that must occur for this person to be happy. The word "and" says that BOTH must be true. If the cafeteria has brownies, but their favorite act loses, they will be unhappy.
"Tomorrow, I will go for a run if I wake up before 7 am and it is not raining."
Again, two conditions must be satisifed. Due to the word "and", it is assumed that if the person wakes up late, then they will not go for a run. Additionally, if it is raining, they will not go for a run.

AND(...) The AND function allows us to see if all specified conditions are met. Consider the following sheet, where each row represents a coupon. Column A is the type of product, and Column B is the discount amount for the coupon.

Imagine at this store, coupons are doubled for all Name brands, but not for coupons over $1. Type into cell C1 =AND(A1="Name" , B1<=1) and fill down to cell C6. The result will be:
Now, in cell D1, type in = IF( AND(A1="Name", B1<=1), B1 * 2, B1) and fill down to cell D6 . The result will be as follows, with column D indicating the actual savings from each coupon at this store.

OR(...) The OR function allows us to see if at least one condition is met. For instance, imagine a climate-controlled building, where each room is its own "zone" with its own system of thermostat and sensors. The policy is that the A/C will be on if the temperature is high or the humidity is high. The temperature threshhold is 72, while the humidity threshhold is 60.

Consider the following small spreadsheet indicating the information on 5 rooms in the building.

1 Room # Temperature Humidity
2 100 70 50
3 105 70 62
4 200 73 55
5 205 70 55
6 210 75 65

In cell D2, we can put IF( OR(B2>72, C2>60), "ON", "OFF") and fill down to D6. The result would be:

1 Room # Temperature Humidity
2 100 70 50 OFF
3 105 70 62 ON
4 200 73 55 ON
5 205 70 55 OFF
6 210 75 65 ON

For rooms 100 and 205, neither condition is met, so the AC is off.

For rooms 105 and 200, one of the two conditions is met, so the AC is on.

For room 210, the temperature is above threshhold and the humidity level is also above threshhold. This is okay, because AT LEAST ONE is true.


Exercises 1 through 4 use the following block of a spreadsheet.

3 Hours 12 10 8 10 9 13

What is displayed by each function?

1. =IF(B3 <> 0, B3*6, "")

2. =IF(D3 <> 0, D3*6, "")

3. =AND( C3 > 10, G3 < 20)

4. =OR( C3 > 10, G3 < 20)

5.Cell A1 contains a number. Write a expression for cell A2 that writes "positive" if the number in A1 is greater than 0 and "not positive" otherwise.

6.A number is in cell B1. Write an expression for cell B2 that puts half of B1's number into B2 if the number is greater than or equal to 0 and puts 0 there otherwise.

7.Cell A1 contains an employee's salary. Write an expression that will calculate the employee's parking fee as follows: For salaries less than $50,000, the parking fee is 1% of the salary. For salaries greater than or equal to $50,000, the fee is $500 plus 1.25% of the amount over $50,000.

8.Cell D5 contains a person's income. Write an expression that calculates a tax on the income, computed as follows. If the income exceeds $40,000, the tax is $800 plus 3% of the amount over $40,000. Otherwise, the tax is 2% of the income.

Credits and licensing

This article is by Robert P. Webber, Scott McElfresh, and Don Blaheta licensed under a Creative Commons BY-SA 3.0 license.

Version 2017-Jan-30 05:00