4 / 6 · Basic
The IF function in a spreadsheet
=IF(B2>=60, "Pass", "Fail") reads like a sentence: if the score is at least 60, write Pass, otherwise write Fail.
Jump to the 10 practice questionsYou will learn
- Write IF with a test and two answers
- Use the six comparison operators
- Return numbers and calculations from IF
Before this: Rounding: ROUND and what the cell shows
1Explore
Try it first: does exactly 60 pass?
Before any rules, open the lab below. Ana scored 72, Ben 45, Chloe exactly 60 and Dev 59.5. Type =IF(B2>=60,"Pass","Fail") in C2 and fill it down. Then change >= to > in C4 and see what happens to Chloe. The formula reads almost like a sentence: if the score is at least 60, write Pass, otherwise write Fail.
Drag to turn. Scroll or pinch to zoom.
2Problem
Ana, 72
Hint
72 is at least 60.
Solution
Answer: Pass
72 ≥ 60 is TRUE, so the second argument is returned.
3Problem
Dev, 59.5
Hint
Just under 60.
Solution
Answer: Fail
59.5 is less than 60, so the third argument is returned.
4Problem
Chloe with >
Hint
60 is not greater than 60.
Solution
Answer: Fail
60 > 60 is FALSE; >= would have been TRUE.
5Explain
What you just typed: a test and two answers
IF takes three arguments: a logical test, the value to return when the test is TRUE, and the value to return when it is FALSE. Microsoft writes it as IF(logical_test, value_if_true, [value_if_false]) and calls it one of the most popular functions in Excel. Sheets uses the same shape. The test is usually a comparison like B2>=60, a tiny formula of its own that gives TRUE or FALSE, and IF picks one of its two answers from that. For Chloe's 60, B4>=60 is TRUE and B4>60 is FALSE. That single difference is what you just saw.
| Comparison | Meaning | With B2 = 72 |
|---|---|---|
| B2=72 | equal to | TRUE |
| B2<>72 | not equal to | FALSE |
| B2>60 | greater than | TRUE |
| B2<60 | less than | FALSE |
| B2>=72 | at least | TRUE |
| B2<=71 | at most | FALSE |
6Explore
The same test in JavaScript
test ? a : b is JavaScript's IF. Pick 60 and compare >= with >.
const score = 60;
console.log(score >= 60 ? "Pass" : "Fail");
console.log(score > 60 ? "Pass" : "Fail");Pass
Fail7Explain
Answers can be numbers
The answers can be text, numbers or calculations. Text goes in double quotes, with one exception Microsoft points out: TRUE and FALSE, which Excel understands on their own. So =IF(C2="Yes",1,2) returns a number, and =IF(C2>B2,C2-B2,0) returns how far over budget a figure is, or 0 when it isn't over. Two quotes with nothing between them, "", return empty-looking text. That is the usual way to leave a cell blank when there is nothing to report.
8Problem
How far over 70
B2 holds 72. What does =IF(B2>70,B2-70,0) return?
Hint
72 − 70.
Solution
Answer: 2
The test is TRUE, so it calculates 72 − 70 = 2.
9Problem
A guard against 0
B2 holds 0. What does =IF(B2=0, "none", A2/B2) return?
Hint
The test is true, so the second argument wins.
Solution
Answer: none
Only the chosen branch is calculated, so the division never happens.
10Explain
Case, blanks and order
Text comparisons ignore upper and lower case, so "yes"="YES" is TRUE. An empty cell compared with "" is also TRUE, which makes =IF(A2="", "missing", A2*2) a handy guard against blanks. Google's help names the most common mistake with IF: putting the two answers in the wrong order. Read the formula aloud as "if this, then that, otherwise the other", and a swap usually jumps out.
11Problem
"yes"="YES"
What does ="yes"="YES" return?
Hint
Text comparisons ignore case.
Solution
Answer: TRUE
Text comparisons ignore upper and lower case.
12Problem
An empty A2
Hint
An empty cell equals "".
Solution
Answer: missing
An empty cell is equal to "", so the test is TRUE.
13Problem
The most common mistake
What is the most common mistake with IF, according to Google's help?
Hint
Two answers swapped.
Solution
Answer: Putting the true and false answers in the wrong order
Sheets' note: provide value_if_true and value_if_false in the correct order.
14Problem
Quote marks
Which formula needs quote marks added to work?
Hint
Yes and No are text.
Solution
Answer: =IF(A1>5,Yes,No)
Yes and No are text; without quotes they give #NAME?. TRUE and FALSE need no quotes.
15Explain
IF down a column
IF works well across a column. Write it once for the first row, with a relative reference to that row's score, and fill down; each student gets a result. Because every row has its own copy, you can sort the list or add students below and fill again. If the test compares with a fixed limit kept in a cell, such as a pass mark, that reference needs dollar signs, exactly like the tax rate in the first unit of this course.
16Explain
Leaving out the third argument
Both programs mark value_if_false as optional. Leave it out, let the test be false, and Excel returns the logical value FALSE, which looks odd in a column of words. Microsoft's list of common IF problems also mentions a 0 in the cell when an answer argument is missing. Write both answers every time, even when one is just "". There is a quieter benefit too. Only the chosen branch is calculated, so =IF(B2=0, "none", A2/B2) never shows #DIV/0!; the division runs only when B2 is not 0.
17Problem
No third argument
In Excel, what does =IF(5>9,"big") return?
Hint
Excel returns a logical value.
Solution
Answer: FALSE
The test is false and there is no third argument, so Excel returns FALSE.
18Explain
IF inside a calculation
An IF doesn't have to end in words. =B2*IF(C2="member",0.9,1) charges members 90% of the price and everyone else the full price. Here IF returns a number and the multiplication uses it like any other value, so one formula per row replaces two separate columns.
19Practice
A member discount
Like =B2*IF(C2="member",0.9,1): finish price(base, kind) so that members pay 90% and everyone else pays the full price.
18
20
8.55function price(base, kind) {
return base;
}
console.log(price(20, "member"));
console.log(price(20, "guest"));
console.log(price(9.5, "member"));
Hint
The value for a true test comes first.
A solution
function price(base, kind) {
return base * (kind === "member" ? 0.9 : 1);
}
console.log(price(20, "member"));
console.log(price(20, "guest"));
console.log(price(9.5, "member"));20Explain
Common mistakes
Using > when the rule says "60 or more".
=IF(B4>60,...) fails a score of exactly 60. "At least" is >=.
Writing =IF(A1>5,Yes,No) without quotes.
Yes and No are text and need quotes; without them the formula shows #NAME?. TRUE and FALSE are the exception.
Swapping the two answers.
=IF(B2>=60,"Fail","Pass") runs without any error and labels every result backwards. Google's help calls the order the most common IF mistake.
Leaving out value_if_false.
When the test is false, Excel shows FALSE in the cell. Write "" if you want it to look empty.
21
Summary
- IF(test, value if TRUE, value if FALSE).
- Comparisons: = <> > < >= <=. Text comparisons ignore case.
- Text answers go in quotes; TRUE and FALSE do not need them.
- Always give both answers; only the chosen one is calculated.