Sub topic 3
The IF Function: Test, Value If True, Value If False
Assume you can enter data in Google Sheets and understand cell references such as A1 and B2. You should also know basic arithmetic operators such as + and *. This chapter uses a score in B2 and labels it based on a pass mark.
The IF function makes a decision. It evaluates a test, then returns one result when the test is true and another result when it is false.
The working model
An IF formula has three arguments:
- A logical test, such as
B2>=50. - The value to return if the test is true.
- The value to return if the test is false.
The basic syntax is:
=IF(logical_test, value_if_true, value_if_false)For example, this formula checks whether the score in B2 is at least 50:
=IF(B2>=50, "Pass", "Fail")If B2 contains 72, the formula returns Pass. If B2 contains 41, it returns Fail.
The leading = tells Sheets that the cell contains a formula. IF is the function name. The parentheses contain the function's arguments, separated by commas.
Build a decision step by step
Suppose your sheet has this data:
A2: a student's nameB2: the student's scoreC2: a result label
Select C2, enter the formula, and press Enter:
=IF(B2>=50, "Pass", "Fail")Sheets evaluates B2>=50 first. A comparison produces either TRUE or FALSE. IF uses that result to choose between the second and third arguments.
Text results must be inside double quotation marks. Without quotation marks, Sheets treats a word such as Pass as a name or reference instead of text.
You can also omit the false result:
=IF(B2>=50, "Pass")When the test is false, this version returns FALSE. Include the third argument when you want a clear result such as Fail, 0, or an empty string.
Choose the right test
The comparison operator determines the decision:
| Operator | Meaning | Example |
|---|---|---|
| `=` | equal to | `A2="Paid"` |
| `<>` | not equal to | `A2<>"Paid"` |
| `>` | greater than | `B2>50` |
| `<` | less than | `B2<50` |
| `>=` | greater than or equal to | `B2>=50` |
| `<=` | less than or equal to | `B2<=50` |
For text, compare the cell with a quoted string:
=IF(A2="Paid", "Done", "Follow up")Text comparison with = is not case-sensitive, so paid also matches Paid.
Do not confuse the comparison operator with the assignment-like syntax used in some programming languages. In Google Sheets, = inside a formula tests equality. The first = in the formula marks the start of the formula.
Understand the returned value
Each branch can return text, a number, a date, or another formula. The two branches do not have to use the same type, but mixing types can make later calculations harder to understand.
=IF(B2>=50, 1, 0)This returns numeric values, which you can total with SUM.
To display nothing when a test is false, return an empty string:
=IF(B2>=50, "Pass", "")An empty string is text with zero visible characters. It is useful when a result column should remain visually blank, although formulas may still treat that cell differently from a truly empty cell.
Common errors
If Sheets reports a formula parse error, check these details:
- Every opening parenthesis has a closing parenthesis.
- Text values use matching double quotation marks.
- Arguments are separated by commas in the formula's locale.
- The comparison is written as part of the test, such as
B2>=50. - Cell references point to the intended cells.
A formula can be syntactically valid but logically wrong. For example, B2>50 does not pass a score of exactly 50. Use B2>=50 when the boundary should count.
Exercise
Write a formula that returns "Eligible" when the value in D2 is 18 or higher, and "Not eligible" otherwise.
Show answer
=IF(D2>=18, "Eligible", "Not eligible")