07 · Logical Functions¶
Logical functions turn a formula from "compute a number" into "make a
decision." This module covers IF, nested IFs, IFS (a cleaner
multi-branch alternative), and combining conditions with AND/OR — using
the budget's Difference and CategoryInfo data from Modules 2 and 6.
1. A single IF¶
- On the
Budgetsheet, add a headerStatusin the next open column (e.g.H1if you've been following along with Modules 4–6's columns). - In
H2(Rent, Difference0), type:=IF(D2>0,"Over Budget","OK") IFtakes three arguments: a condition (D2>0), the value if TRUE ("Over Budget"), and the value if FALSE ("OK"). Rent's Difference is0, so0>0isFALSE, and the cell showsOK.- Copy
H2down. Groceries (55) and Entertainment (75) should showOver Budget; Rent, Transport (-20), and Savings should showOK— note this simple version doesn't yet distinguish "exactly on budget" from "under budget," which the nested version in Section 2 fixes.
2. Nested IF for three outcomes¶
- A single
IFonly branches two ways. To get three outcomes — Over, Under, and On Track — nest a secondIFinside the FALSE branch of the first:=IF(D2>0,"Over Budget",IF(D2<0,"Under Budget","On Track")) - Reading it: if Difference is greater than 0, "Over Budget"; otherwise, check a second condition — if Difference is less than 0, "Under Budget"; otherwise (meaning it's neither greater nor less than 0, so it must be exactly 0), "On Track".
- Copy this down all six rows. Expected results: Rent
On Track, GroceriesOver Budget, TransportUnder Budget, EntertainmentOver Budget, SavingsOn Track, SubscriptionsOver Budget(5from Module 4's added row). - Nesting more than two or three
IFs becomes hard to read fast — each additional branch adds another layer of parentheses to track. This is exactly the problemIFS(Section 3) solves.
3. IFS for cleaner multi-branch logic¶
- On the
CategoryInfosheet, add a headerLevelinD1. InD2(Rent, Priority1), type:=IFS(C2<=2,"High",C2<=4,"Medium",TRUE,"Low") IFStakes pairs of (condition, result) and returns the result for the first condition that'sTRUE, checked top to bottom — no nesting or extra parentheses per branch.TRUEas a final condition acts as a catch-all "else," equivalent to the innermostELSEof a nestedIF.- Copy
D2down through row 7. Expected: Rent (Priority1) →High, Savings (2) →High, Subscriptions (3) →Medium, Groceries (4) →Medium, Transport (5) →Low, Entertainment (6) →Low. - If no condition in an
IFSmatches and there's noTRUEcatch-all, the formula returns#N/A— always include a final catch-all branch unless you specifically want that error as a signal that a case was missed.
4. Combining conditions with AND / OR¶
- Back on the
Budgetsheet, add a headerNeeds Reviewin the next column. Type:=IF(AND(E2="Variable",D2>50),"Review","")(using theTypecolumnEpulled viaVLOOKUPin Module 6, andDifferencecolumnD). AND(condition1,condition2,…)returnsTRUEonly if every condition isTRUE. Here: the category must beVariableand its Difference must exceed50.- Copy down. Groceries (
Variable,55) and Entertainment (Variable,75) should showReview; Transport (Variable,-20) does not, since-20>50isFALSE, failing theAND; Rent and Savings (Fixed) never showReviewregardless of their Difference, since theTypecondition alone already fails. OR(condition1,condition2,…)returnsTRUEif any condition isTRUE. SwapANDforORin the same formula —=IF(OR(E2="Variable",D2>50),"Review","")— and everyVariablecategory flags regardless of its Difference (since the first condition alone satisfiesOR), plus anyFixedcategory whose Difference happens to exceed50(none do here, but the logic would catch one if it existed).AND/ORcan nest insideIF, orIFcan nest insideAND/OR's arguments — but as with nestedIFs, past two or three conditions, naming intermediate results in helper columns (e.g. a plainTRUE/FALSEcolumn for each condition) keeps a workbook easier to audit than one dense formula.
Cheat sheet¶
| Function | Syntax | Behavior |
|---|---|---|
IF |
=IF(condition,if_true,if_false) |
Two-way branch |
Nested IF |
=IF(c1,r1,IF(c2,r2,r3)) |
Three+ way branch, gets unwieldy fast |
IFS |
=IFS(c1,r1,c2,r2,TRUE,default) |
Cleaner multi-branch, first match wins |
AND |
=AND(c1,c2,…) |
TRUE only if all conditions are TRUE |
OR |
=OR(c1,c2,…) |
TRUE if any condition is TRUE |
How It Actually Works¶
IF, AND, OR, and their relatives are all built on Excel's Boolean
type, which internally is stored as a 1 or 0 but displays as TRUE/FALSE
— that's why =TRUE+TRUE evaluates to 2 and why SUM of a range
containing logical values (via -- or arithmetic coercion) can be used to
count matches. Nested IF statements are evaluated with short-circuit
lazy evaluation: Excel only computes the branch it actually needs. Given
=IF(A1>0, expensive_formula_1, expensive_formula_2), only one of the two
branches is ever calculated for that cell — this is why wrapping a
division in IF(B2=0, "", A2/B2) safely avoids a #DIV/0! error rather
than computing the division first and discarding it: the division branch is
never entered when the condition is false. AND and OR, by contrast, are
not short-circuited in the traditional programming sense — they take an
array of arguments and Excel generally evaluates all of them before
combining the results, which is why an AND(A1<>0, B1/A1>2) can still throw
#DIV/0! even though the first condition would have prevented an unsafe
divide in a short-circuiting language.
Exercise¶
Add a Status column to the Budget sheet using the three-way nested
IF from Section 2, and confirm every row's result matches the expected
list. Add a Level column to CategoryInfo using IFS based on
Priority, confirming Rent/Savings are High, Subscriptions/Groceries are
Medium, and Transport/Entertainment are Low. Finally add a
Needs Review column using AND(Type="Variable", Difference>50) and
confirm only Groceries and Entertainment flag.