Sub topic 1
Logical tests: the building block of every branch
Prerequisites
This chapter assumes you can enter and edit formulas in Google Sheets, reference cells such as B2 and ranges such as A:A, use comparison operators (=, <>, >, >=, <, <=), and write a basic IF formula.
Logical tests
A logical test is an expression that evaluates to TRUE or FALSE. Every branching function in Sheets starts with one. For example, B2>=90 returns TRUE when B2 holds 90 or higher.
=IF(B2>=90, "A", "B")If B2 holds 95, the formula returns "A". If B2 holds 85, it returns "B".
Why nested IF gets hard to read
To handle more than two outcomes, you can nest one IF inside another. This works, but each new tier adds another level of parentheses, and those are easy to miscount.
=IF(B2>=90, "A", IF(B2>=80, "B", IF(B2>=70, "C", "F")))Four grades need three IF functions and three closing parentheses. Adding a fifth grade means editing the middle of the formula, which is where mistakes tend to appear.
IFS: testing several conditions in order
IFS takes pairs of arguments: a condition, then the value to return when that condition is true. It checks the pairs from left to right and returns the value for the first true condition.
=IFS(B2>=90, "A", B2>=80, "B", B2>=70, "C", TRUE, "F")The same grades now read as a flat list of pairs.
IFS order and a TRUE fallback
Order matters. Because the first true condition wins, this version gives the wrong answer for a score of 95:
=IFS(B2>=70, "C", B2>=80, "B", B2>=90, "A", TRUE, "F")B2>=70 is already true for 95, so the formula returns "C".
The final TRUE pair acts as a fallback. TRUE is always true, so any value that reaches it gets the default result. Without it, a score of 65 matches no condition, and IFS returns #N/A.
SWITCH: mapping exact values to results
SWITCH compares one expression with a list of exact values. The first argument is the expression, followed by value and result pairs. An unpaired last argument serves as the default when nothing matches.
=SWITCH(A2, "NY", "New York", "CA", "California", "TX", "Texas", "Unknown")If A2 holds "CA", the formula returns "California". If A2 holds "WA", it returns "Unknown". Use SWITCH for exact matches. Use IFS when conditions involve ranges such as >= or <.
Exercise
In the IFS formula with the conditions in the wrong order, what does B2 = 95 return, and why?
Show answer
It returns "C". The first condition, B2>=70, is true for 95, so IFS stops there and never reaches the "A" pair.