Sub topic 2
Why nested IF gets hard to read
This chapter assumes you can enter and edit Google Sheets formulas, use cell references such as B2, write comparison operators such as >=, and use a basic IF formula.
A nested IF is an IF formula placed inside another IF formula. It lets one formula make several decisions. For example, suppose B2 contains a student's score:
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C","F")))Sheets evaluates the formula from the outside inward:
- If
B2is at least 90, return"A". - Otherwise, test whether
B2is at least 80. - Otherwise, test whether
B2is at least 70. - If none of those tests is true, return
"F".
A score of 85 returns B. A score of 62 returns F.
The building block: a logical test
A logical test is an expression that produces either TRUE or FALSE. Comparison operators create logical tests:
=B2>=90If B2 contains 95, this test is TRUE. If it contains 72, the test is FALSE.
The IF function uses this result to choose between two values:
=IF(B2>=70,"Pass","Fail")The first argument is the logical test. The second argument is returned when the test is true. The third argument is returned when it is false. Nesting adds another IF as one of those results.
Why nesting becomes difficult
Each additional decision adds another condition, another result, and another closing parenthesis. A formula with many levels is difficult to scan:
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C",IF(B2>=60,"D","F"))))The main problems are:
- The decision structure is hidden inside one long line.
- Matching opening and closing parentheses becomes error-prone.
- Changing one score range can accidentally change a different branch.
- It is easy to omit a final fallback result.
- Another person must trace each false path to understand the formula.
- Conditions can overlap, so their order affects the result.
The formula is technically valid, but its structure is not obvious. Indenting the formula can help you inspect it:
=IF(
B2>=90,
"A",
IF(
B2>=80,
"B",
IF(
B2>=70,
"C",
"F"
)
)
)Google Sheets accepts the line breaks and indentation when you enter them in the formula editor. The formatting does not change how the formula evaluates.
Read the branches as a decision tree
A useful way to understand a nested IF is to draw each test as a branch. The formula checks the first condition before it checks any later condition.
This structure works because the thresholds descend from highest to lowest. A score of 95 satisfies every threshold, but the first true test returns A immediately.
A common mistake is to put the broadest condition first:
=IF(B2>=70,"C",IF(B2>=80,"B",IF(B2>=90,"A","F")))With this order, a score of 95 returns C. The formula never reaches the later tests because B2>=70 is already true.
> callout warning > When conditions overlap, the first true branch wins. Order nested IF tests from the most specific or highest-priority condition to the least specific one.
When to replace nested IF
Nested IF is reasonable for a small number of decisions. Consider another function when the formula contains several tiers or when people will maintain it later.
For ordered ranges, IFS lists each condition and result as a pair:
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")This has the same decision order but makes the pairs easier to identify. The final TRUE,"F" pair provides the fallback. The next chapter develops this pattern.
Exercise
What does =IF(B2>=70,"C",IF(B2>=80,"B","F")) return when B2 contains 85, and why?
Show answer
It returns "C" because B2>=70 is tested first and is already TRUE. The later B2>=80 test is never evaluated.