Sub topic 1
Logical tests: the building block of every branch
Every branching formula in Google Sheets rests on one idea: a test that comes out TRUE or FALSE. IF, IFS, and SWITCH differ in how they arrange tests and results. This chapter covers the tests themselves, so the later chapters on IFS, SWITCH, and IFERROR have a solid base.
Prerequisites
This chapter assumes you can already do the following:
- Enter and edit formulas in Google Sheets, including starting a formula with
=. - Use cell references and ranges such as
B2andA:A. - Read the comparison operators
=,<>,>,>=,<, and<=. - Use the basic
IFfunction with a test, a value if true, and a value if false.
If any of these feel shaky, review them first. Everything below builds on them.
What a logical test is
A logical test is any expression that Sheets can evaluate to one of two Boolean values: TRUE or FALSE. A Boolean is a value with only two possible states.
You can type a test into a cell on its own. If B2 contains 85, then:
| Formula | Result |
|---|---|
| `=B2>=90` | FALSE |
| `=B2>=80` | TRUE |
| `=B2<>85` | FALSE |
| `=B2=85` | TRUE |
Sheets displays Boolean results as TRUE or FALSE, centered in the cell by default. They are real values, not text. A cell containing the word "TRUE" typed inside quotes is a different thing, and the difference causes bugs later.
How IF consumes a test
The IF function takes a test and picks one of two results.
=IF(B2>=60,"Pass","Fail")Sheets evaluates B2>=60 first. If the result is TRUE, it returns "Pass". If the result is FALSE, it returns "Fail". IFS and SWITCH follow the same pattern. They evaluate tests, then choose a result.
Comparison operators in practice
The six comparison operators produce most of your tests.
| Operator | Meaning | Example | Result when A1 is 5 |
|---|---|---|---|
| `=` | equal to | `A1=5` | TRUE |
| `<>` | not equal to | `A1<>5` | FALSE |
| `>` | greater than | `A1>5` | FALSE |
| `>=` | greater than or equal to | `A1>=5` | TRUE |
| `<` | less than | `A1<5` | FALSE |
| `<=` | less than or equal to | `A1<=5` | TRUE |
A few details affect real formulas:
- Text comparisons with
=ignore case.="ny"="NY"returnsTRUE. - Text in a formula goes in double quotes.
A2="NY"compares to the text NY.A2=NYmakes Sheets look for a named range called NY, which produces a#NAME?error. - Numbers and text do not match. A cell holding the number 5 does not equal the text
"5". Numbers stored as text are a common cause of tests that seem to fail for no reason. >and<also work on text and compare alphabetically, which is rarely what you want in a grading or pricing formula.
Testing for blank cells
A blank cell is a special case. The test A2="" returns TRUE for a truly empty cell and also for a cell containing an empty string. The function ISBLANK(A2) returns TRUE only for a truly empty cell.
A blank cell also behaves like 0 in numeric comparisons. If B2 is empty, B2<60 returns TRUE, and a formula like =IF(B2<60,"Fail","Pass") marks the empty row as a failure. Guard against this when blanks are possible:
=IF(B2="","",IF(B2>=60,"Pass","Fail"))Functions that return TRUE or FALSE
Comparison operators are not the only source of tests. Many functions return Booleans, and you can use them anywhere a test is expected.
| Function | Returns TRUE when |
|---|---|
| `ISNUMBER(A2)` | A2 holds a number |
| `ISTEXT(A2)` | A2 holds text |
| `ISBLANK(A2)` | A2 is truly empty |
| `ISERROR(A2)` | A2 holds any error value |
| `ISNA(A2)` | A2 holds the `#N/A` error |
| `ISEVEN(A2)` | A2 is an even number |
For example, =IF(ISNUMBER(B2),B2*1.1,"Not a number") raises a price by 10 percent only when B2 really is a number.
Combining tests with AND, OR, and NOT
One comparison is often not enough. Three functions combine tests into a single result:
AND(test1, test2, ...)returnsTRUEonly if every test isTRUE.OR(test1, test2, ...)returnsTRUEif at least one test isTRUE.NOT(test)flipsTRUEtoFALSEand the reverse.
=IF(AND(B2>=60,C2>=60),"Pass both","Retake")
=IF(OR(A2="NY",A2="CA"),"Coastal","Other")
=IF(NOT(ISBLANK(B2)),"Has score","Missing")The first formula passes a student only if both scores reach 60. The second labels a row coastal if the state is either of two values. The third checks that a score exists.
Each of these functions returns a single Boolean, so the whole combination still counts as one test. IF, IFS, and SWITCH(TRUE, ...) accept it in the same place as B2>=60.
Booleans as numbers
Sheets treats TRUE as 1 and FALSE as 0 when you do arithmetic on them. Writing =TRUE+TRUE returns 2. This lets you count matches by adding tests:
=SUM(ARRAYFORMULA(B2:B10>=60))Here ARRAYFORMULA evaluates the test for every cell in the range, and SUM adds the resulting TRUE values as 1s. Without ARRAYFORMULA, the array evaluation may not happen in a plain SUM, so keep it in this form. The result is the number of scores of at least 60.
The conversion can also surprise you. In IF, a number used as the test is treated as TRUE if it is non-zero and FALSE if it is zero. =IF(5,"yes","no") returns "yes", and =IF(0,"yes","no") returns "no". That is useful when a cell already holds a count, but it hides intent. Writing the comparison out, as in IF(C2>0, ...), is clearer.
Common mistakes with tests
- Forgetting quotes around text.
A2=NYraises#NAME?. UseA2="NY". - Comparing text numbers to numbers. If a score was pasted as text,
B2>=60can give an unexpected answer. Check withISNUMBER(B2). - Using one
=where you need a comparison inside a larger expression. Inside a formula,=is the comparison operator, soIF(A2=5, ...)is correct. You do not need a double equals sign as in some programming languages. - Treating blanks as real values. An empty cell counts as 0 in numeric tests and as an empty string in text tests.
- Writing chained comparisons.
60<=B2<=100does not work as it does in math. WriteAND(B2>=60,B2<=100).
Why this matters for the rest of the tutorial
A nested IF is a stack of tests, each with its own results. IFS puts those tests in one flat list. SWITCH replaces the tests with a single exact-match comparison. SWITCH(TRUE, ...) returns to using full tests. IFERROR waits for a test to fail so that it can return a fallback. If you can predict what each test returns for a given input, you can predict what the larger formula returns.
Exercise
Cell B2 holds 72 and cell C2 is empty. What does each formula return? (a) =B2>=70 (b) =AND(B2>=70,C2>=70) (c) =OR(B2>=70,C2>=70) (d) =C2<70
Show answer
(a) TRUE, because 72 is at least 70. (b) FALSE, because C2 is empty and counts as 0 in a numeric comparison, so C2>=70 is FALSE and AND needs both tests to be TRUE. (c) TRUE, because OR needs only one TRUE test and B2>=70 is TRUE. (d) TRUE, because an empty cell counts as 0 and 0 is less than 70.
Exercise
A formula =IF(A2=CA,"West","Other") returns #NAME?. What is wrong, and what is the corrected formula?
Show answer
The text CA is not in double quotes, so Sheets reads it as a name that does not exist. The corrected formula is =IF(A2="CA","West","Other").
Exercise
Write a single test that is TRUE only when B2 is a number from 0 to 100 inclusive.
Show answer
=AND(ISNUMBER(B2),B2>=0,B2<=100). The ISNUMBER check keeps text and other non-numeric values from passing the range comparisons.