Sub topic 2
Why nested IF gets hard to read
This chapter explains why a long nested IF formula becomes hard to read and maintain, and how to spot the point where you should switch to something else. It assumes you can enter and edit formulas in Google Sheets, use cell references such as B2 and ranges such as A:A, use the comparison operators (=, <>, >, >=, <, <=), and write a basic IF. IFS and SWITCH, the replacements, come in later chapters. Here the goal is to understand the problem well enough to recognize it.
What nesting means
IF takes three arguments: a test, a value to return if the test is true, and a value to return if it is false.
=IF(test, value_if_true, value_if_false)The third argument can be any value, including another IF. Putting one IF inside another is called nesting. It lets one formula choose between more than two outcomes.
Here is a nested IF that turns a score in B2 into a letter grade:
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C","F")))Sheets evaluates it from left to right:
- If
B2is at least 90, return"A"and stop. - Otherwise, test whether
B2is at least 80. If so, return"B"and stop. - Otherwise, test whether
B2is at least 70. If so, return"C". - Otherwise, return
"F".
The formula works, and for three tiers it is still manageable. The trouble starts when the number of tiers grows.
Each diamond is one IF. Each "No" arrow leads into the next IF, which sits inside the previous one's third argument. That is why the formula nests deeper with every tier.
Where readability breaks down
Nested IF formulas become hard to read for four separate reasons.
The closing parentheses pile up
Every IF needs its own closing parenthesis, and all of them land at the very end of the formula. The three-tier grade formula ends in ))). A formula with eight tiers ends in eight. When you add a tier, you must add a matching parenthesis at the end, far from the part you edited. Miss one and Sheets reports a parse error. Add an extra and you get the same.
A value and its test are far apart
Look at the formula again:
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C","F")))The test B2>=90 is next to its result "A", which is good. But the final "F" has no test beside it. To know when "F" is returned, you must read every test before it and reason that all of them failed. The last value in a nested IF is defined only by the absence of everything else.
The structure is not visible
A nested IF looks like one flat string of text. The real structure is a chain of decisions, but nothing in the text shows that. Compare a longer example:
=IF(B2>=97,"A+",IF(B2>=93,"A",IF(B2>=90,"A-",IF(B2>=87,"B+",IF(B2>=83,"B",IF(B2>=80,"B-","F"))))))Reading this, you have to count parentheses and track which IF you are inside. Most people give up and rely on trial and error.
Editing is risky
Changing one tier means working in the middle of a long line. Inserting a tier means finding the correct position, adding text there, and adding a closing parenthesis at the end. A small slip changes the meaning of the formula without producing an error, which is the worst case, because you may not notice.
Formatting helps, but only a little
Google Sheets lets you add line breaks inside a formula. In the formula bar, press Ctrl+Enter (or Cmd+Enter on a Mac) to insert a line break. Spaces and line breaks outside quoted text are ignored by the formula parser, so you can lay the formula out to show its structure:
=IF(B2>=90, "A",
IF(B2>=80, "B",
IF(B2>=70, "C",
"F"
)
)
)This makes the nesting visible, and it is worth doing when you must keep a nested IF. It does not fix the underlying issues. The closing parentheses are still stacked at the end, and each new tier still pushes the formula further to the right.
Failure modes in nested IF
Most nested IF mistakes fall into a few patterns. Knowing them helps you debug faster.
Tests in the wrong order
Because Sheets stops at the first true test, order matters. Consider the same grade formula with the tests reversed:
=IF(B2>=70,"C",IF(B2>=80,"B",IF(B2>=90,"A","F")))A score of 95 is at least 70, so the first test is true and the formula returns "C". The tests for 80 and 90 are never reached. The formula has no error, but it gives the wrong answer for every score of 70 or above.
The rule for range-based tests that use >= is to put the strictest test first and work down. For tests that use < or <=, put the smallest limit first and work up.
Missing or extra parentheses
If a parenthesis is missing, Sheets often tries to fix it for you by adding the closing one when you press Enter. That can hide a mistake about where the parenthesis belonged. If you see a formula parse error, count the opening and closing parentheses first. Line breaks as shown above make the count easier.
Forgetting the final value
IF allows you to leave out the third argument. If you do, Sheets returns FALSE when the test fails. Consider:
=IF(B2>=90,"A",IF(B2>=80,"B"))A score of 75 fails both tests, and the inner IF has no third argument, so the cell shows FALSE. A grade column that shows FALSE for some rows is a sign that a nested IF lacks a final value.
Mixing types in tests
A score typed as text, such as "85" stored as a string, does not behave like the number 85 in comparisons. Sheets treats text as greater than any number, so a text value can make B2>=90 true. Check that your input column holds real numbers. Numbers are right-aligned by default, and text is left-aligned, which is a quick visual check.
The limits of nesting
Sheets allows many levels of nesting, so you will usually hit the readability limit long before any technical one. A better test is practical. Ask yourself:
- Can a colleague read the formula and say what each tier does without counting parentheses?
- If a tier changes, can you edit it with confidence?
- Do you need to add tiers later?
If any answer is no, the formula has outgrown nested IF.
There is also a duplication problem. If the same decision logic appears in a nested IF across several columns, a change in the rules means editing every copy. A formula that is hard to read is also hard to keep consistent.
What to use instead
The two functions covered next in this tutorial each solve a different part of the problem.
| Situation | Nested IF problem | Replacement |
|---|---|---|
| Several range-based tests, such as grade bands | Parentheses pile up, tests are separated from the fallback | IFS lists condition and value pairs side by side |
| One value compared with exact matches, such as state codes | The same cell is repeated in every test | SWITCH names the value once and lists the cases |
IFS keeps each condition next to its result and needs no closing parentheses for each tier. The grade formula becomes:
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")You do not need to understand every detail of that formula yet. Notice only that it is flat. Each tier is a pair, and adding a tier means adding one more pair in the right place. The next chapter explains how it works.
Nested IF is not wrong. For a simple two-way choice, or three outcomes at most, IF is clear and the right tool. The point of this chapter is to recognize when a formula has grown past that.
A short worked example
Suppose a shipping sheet calculates a fee from order weight in B2, in kilograms:
- Under 1 kg: 4
- 1 kg up to but not including 5 kg: 7
- 5 kg up to but not including 20 kg: 12
- 20 kg or more: 25
As a nested IF with the smallest limit first:
=IF(B2<1,4,IF(B2<5,7,IF(B2<20,12,25)))Walk through a weight of 5:
5<1is false, so move to the innerIF.5<5is false, so move on.5<20is true, so the result is 12.
The boundary matters. Because the second test uses < and not <=, a weight of exactly 5 falls into the third tier. If you changed it to B2<=5, a weight of 5 would cost 7. When you read or write any nested IF, check each boundary value by hand, because off-by-one mistakes at the edges are the most common logic error.
Now consider the cost of adding a fifth tier for weights of 50 kg or more at a fee of 40. You must add a test at the end, add a parenthesis, and change the final value:
=IF(B2<1,4,IF(B2<5,7,IF(B2<20,12,IF(B2<50,25,40))))The change touched the last tier and the closing parentheses, and the number 25, once the fallback, now has a test of its own. This kind of rewiring in several places is what makes nested IF error-prone.
Summary
- Nested IF places each new
IFin the false branch of the one before it. - Readability suffers because parentheses stack at the end, the fallback value has no visible test, and the structure is hidden in a flat line.
- Evaluation stops at the first true test, so wrong order gives wrong answers without any error.
- A missing third argument returns
FALSE. - Line breaks help but do not remove the underlying problem.
- Keep nested IF for two or three outcomes, and use IFS or SWITCH beyond that.
Exercise
A sheet uses =IF(B2>=70,"C",IF(B2>=80,"B",IF(B2>=90,"A","F"))) to grade scores. A student with a score of 92 gets "C". Explain why, and rewrite the formula so that it gives the correct grade for 92 and for a score of 65.
Show answer
Sheets evaluates the tests in order and stops at the first true one. A score of 92 is at least 70, so the first test is true and the formula returns "C" without checking the later tests. The strictest test must come first: =IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C","F"))). With this order, 92 returns "A" and 65 fails all three tests and returns "F".
Exercise
What does =IF(B2>=90,"A",IF(B2>=80,"B")) show in the cell when B2 contains 75, and why?
Show answer
It shows FALSE. The score fails both tests, and the inner IF has no third argument, so IF returns FALSE by default when its test fails. Adding a final value, such as IF(B2>=80,"B","F"), fixes it.
Exercise
In the shipping fee formula =IF(B2<1,4,IF(B2<5,7,IF(B2<20,12,25))), what fee does a weight of exactly 20 receive, and what fee would a weight of exactly 1 receive?
Show answer
A weight of 20 receives 25, because 20<20 is false and the formula reaches the final value. A weight of exactly 1 receives 7, because 1<1 is false, then 1<5 is true.