Sub topic 3
The IF function: test, value if true, value if false
IF makes a formula choose between two results. It checks a condition, then returns one value if the condition holds and another if it doesn't. Most decision-making in a spreadsheet starts here, and later tools such as AND, IFS and IFERROR build on it.
This chapter assumes you can enter data in cells, refer to cells such as A1 or B2, and use basic operators such as + and *. It also assumes you know the comparison operators (=, <>, >, <, >=, <=) from the previous chapter.
The three arguments
An argument is a value you pass into a function. IF takes three, separated by commas:
=IF(logical_expression, value_if_true, value_if_false)| Argument | Role | Required |
|---|---|---|
| `logical_expression` | The test. It must produce TRUE or FALSE. | Yes |
| `value_if_true` | What the formula returns when the test is TRUE. | Yes |
| `value_if_false` | What the formula returns when the test is FALSE. | No |
Here is the simplest useful case. Suppose B2 holds an exam score and 50 is the pass mark:
=IF(B2>=50, "Pass", "Fail")Sheets works through it in order:
- It evaluates
B2>=50. IfB2is 72, the result is TRUE. - Because the test is TRUE, it returns the second argument,
"Pass". - The third argument,
"Fail", is ignored.
If B2 is 31, the test is FALSE and the cell shows Fail.
Writing the test
The first argument can be any expression that evaluates to TRUE or FALSE. The usual form is two values joined by a comparison operator. Each side can be a cell reference, a typed value, or another formula:
B2>=50compares a cell to a number.B2>=F1compares a cell to another cell.B2>AVERAGE(B2:B30)compares a cell to the result of a function.D2<TODAY()compares a date to today's date.
You can check what a test returns by typing it alone in a cell, for example =B2>=50. Doing this is a quick way to debug a formula that gives the wrong answer, because you see the TRUE or FALSE that IF is acting on.
Text in the test
Text values must be wrapped in straight double quotes:
=IF(A2="Paid", "Done", "Follow up")Comparing text with = is not case-sensitive, so paid, PAID and Paid all match. The match is otherwise exact. A cell containing Paid with a trailing space does not equal "Paid". Stray spaces are a common reason for a test that looks right but returns FALSE. TRIM(A2) removes them.
Numbers and other values as the test
The test does not have to be a comparison. Sheets also accepts a cell or formula that already returns TRUE or FALSE, such as a checkbox cell. For example, if C2 is a checkbox:
=IF(C2, "Complete", "Open")A number used as the test follows a simple rule: 0 counts as FALSE and any other number counts as TRUE. A text value that is not a boolean, such as "abc", makes IF return a #VALUE! error. Prefer an explicit comparison when you can, because it shows your intent.
What the two result arguments can contain
value_if_true and value_if_false accept the same kinds of things:
- Text in quotes, such as
"Pass". - Numbers, such as
100or0. - Cell references, such as
C2. - Formulas, such as
B2*0.1. - Another
IF(covered in the chapter on nesting).
This lets you return a calculated value on one branch and a fixed value on the other:
=IF(B2>=1000, B2*0.1, 0)This gives a 10% bonus on sales of 1000 or more and nothing otherwise. Returning text and numbers from the same formula is allowed, but a column that mixes the two is harder to sum or sort later. Choose one type per column where you can.
Only the branch that is selected gets evaluated. This is useful, because the unselected branch can contain an expression that would fail:
=IF(C2=0, "n/a", B2/C2)When C2 is 0, the division never runs, so you do not get #DIV/0!. This is a better first choice than hiding the error afterwards, which a later chapter on IFERROR covers.
Leaving out the third argument
The third argument is optional. If you omit it and the test fails, the formula returns the boolean FALSE:
=IF(B2>=50, "Pass")For a score of 72 the cell shows Pass. For a score of 31 the cell shows FALSE. That is rarely what you want in a report, so provide the third argument unless you really need a boolean. If you want the cell to look empty on the false branch, use an empty string, "". The next chapter covers that in detail.
Making the threshold easy to change
Typing 50 into the formula is called hard-coding a value. It works, but changing the pass mark means editing every formula. A better habit is to keep the threshold in its own cell and refer to it. Suppose the pass mark is in F1:
=IF(B2>=$F$1, "Pass", "Fail")The dollar signs make the reference absolute, so it still points at F1 when you copy the formula down. Without them, the reference would shift to F2, F3 and so on, and the results would be wrong from the second row. Relative and absolute references get their own treatment later, but remember this pattern now, because it is the most common reason a copied IF formula breaks.
Edge cases to know
These behaviors surprise people the first time.
Blank cells count as zero in numeric comparisons. If B2 is empty, B2>=50 is FALSE, so =IF(B2>=50, "Pass", "Fail") returns Fail for a student who has no score yet. Whether that is acceptable depends on your sheet. A later chapter shows how to test for blanks.
Text compares as larger than numbers. If B2 contains the text absent rather than a number, then B2>=50 is TRUE, because Sheets orders text above numbers. The formula returns Pass for a student marked absent. If your data mixes numbers and text, add a check such as ISNUMBER(B2) to the test. Combining checks is the subject of the chapter on AND and OR.
Numbers stored as text. A value that looks like 72 but is left-aligned and stored as text may not compare the way you expect. Compare =B2>=50 in a spare cell. If the result is TRUE for something that should be FALSE, check the cell's format and content.
Argument separators vary by locale. Spreadsheets set to some regional settings use semicolons instead of commas, as in =IF(B2>=50; "Pass"; "Fail"). If Sheets rejects your pasted formula, check the spreadsheet's locale under File, Settings.
Quotes must be straight. Text copied from a document or web page may contain curly quotes (“ ”). Sheets treats those as part of a name, not as text delimiters, and the formula fails with a parse error or #NAME?. Retype the quotes in the formula bar.
A worked example
Take this small table, with the pass mark in F1 set to 50:
| Row | A | B | C |
|---|---|---|---|
| 1 | Name | Score | Result |
| 2 | Ana | 72 | |
| 3 | Ben | 31 | |
| 4 | Chloe | 50 |
In C2, enter:
=IF(B2>=$F$1, "Pass", "Fail")Fill it down to C4. The results are:
| Name | Score | Test (`B>=50`) | Result |
|---|---|---|---|
| Ana | 72 | TRUE | Pass |
| Ben | 31 | FALSE | Fail |
| Chloe | 50 | TRUE | Pass |
Chloe's score equals the threshold. She passes because the operator is >=. If you wrote > instead, she would fail. Boundary values are where operator mistakes show up, so test them whenever you write a rule.
Quick checklist for a working IF
Before you trust an IF formula, check that:
- The test returns TRUE or FALSE when you evaluate it alone.
- Every piece of text is inside straight double quotes.
- Every opening parenthesis has a closing one.
- The commas separate exactly three arguments (or two, if you accept FALSE as the default).
- Any threshold cell uses an absolute reference such as
$F$1. - You have tried a value exactly on the boundary, and a blank cell.
Exercise
Cell B2 holds a student's score and cell F1 holds the pass mark. Write a formula that returns "Pass" when the score is at or above the pass mark and "Retake" otherwise. It will be copied down column C. What does it return for a score that equals the pass mark, and what would it return if you wrote B2>$F$1 instead?
Show answer
=IF(B2>=$F$1, "Pass", "Retake"). The $F$1 reference is absolute, so it stays on the pass mark when copied down. A score equal to the pass mark returns "Pass" because >= includes equality. With B2>$F$1, a score equal to the pass mark returns "Retake", because the test is FALSE when the two values are equal.
Exercise
A colleague enters =IF(B2>=50, "Pass") and says some rows show FALSE instead of text. Explain why this happens and give a corrected formula that shows "Fail" in those rows.
Show answer
The third argument, valueiffalse, is missing. When the test fails, IF returns the boolean FALSE by default. The corrected formula is =IF(B2>=50, "Pass", "Fail").
Exercise
A sheet contains the text "absent" in B5. What does =IF(B5>=50, "Pass", "Fail") return for that row, and why? Name one way to avoid the problem.
Show answer
It returns "Pass", because Sheets treats text as larger than any number, so "absent">=50 is TRUE. To avoid this, test that the cell holds a number before comparing, for example by using ISNUMBER(B5) together with the comparison (combining tests is covered in the AND/OR chapter).