IF, AND, OR and NOT
Write IF formulas that return text, numbers or blanks, and combine several conditions with AND, OR and NOT to model real business rules.
Welcome back!
Step 1 of 11Learn
A rule your spreadsheet can follow
An online store offers free shipping on orders of $50 or more; smaller orders pay $7.95. With 2,000 orders a month, nobody wants to check each one by hand. The rule is a decision, and IF is how a spreadsheet makes decisions:
=IF(logical_test, value_if_true, value_if_false)
For an order total in B2:
=IF(B2>=50, 0, 7.95)
Excel evaluates the test B2>=50. If it's TRUE, the formula returns the second argument, 0. If it's FALSE, it returns the third, 7.95. Copy it down, and every order gets the right charge.
Read an IF aloud as a sentence: if the total is at least 50, shipping is 0, otherwise 7.95. If the sentence sounds wrong, the formula is wrong. Most IF bugs are boundary bugs: using > when the rule says "or more" means a $50.00 order pays shipping it shouldn't. When you write a test, check the exact boundary value, not just values far from it.
Step 2 of 11Learn
What IF can return
The second and third arguments can be almost anything:
- Text, in double quotes:
=IF(C2>=70,"Pass","Fail") - Numbers, without quotes:
=IF(B2>=50,0,7.95). Writing"0"returns text that looks like zero but isn't counted bySUM. - Calculations:
=IF(B2>=1000, B2*5%, B2*2%)pays 5% commission on big orders and 2% on the rest. - An empty-looking cell:
""(two quotes) returns empty text, often used to hide a result until data arrives, as in=IF(A2="","",A2*1.08). - Another function, including another
IF, which you'll meet in the next lesson.
If you leave out the third argument, =IF(B2>=50,"Free") returns FALSE when the test fails, which looks sloppy in a report. Always supply both outcomes.
One more subtlety: blank cells count as 0 in numeric tests. =IF(B2>=50,0,7.95) charges $7.95 shipping on an empty row. If blank rows are possible, test for them first, which is exactly what IF(A2="","",...) does.
Step 3 of 11Practice · Work it out
B2 holds an order total of 49.99. What does =IF(B2>=50, 0, 7.95) return?
Step 4 of 11Learn
AND, OR and NOT
Real rules usually combine conditions. Three logical functions handle that, and each returns TRUE or FALSE:
AND(test1, test2, ...)is TRUE only if every test is TRUE.OR(test1, test2, ...)is TRUE if at least one test is TRUE.NOT(test)flips TRUE to FALSE and back.
Suppose free shipping applies to orders of $50 or more or to loyalty members (column C says "Member"). The test is OR(B2>=50, C2="Member"), and the full formula is:
=IF(OR(B2>=50, C2="Member"), 0, 7.95)
Now a stricter promotion: members who order at least $50 get a free gift. That needs both conditions:
=IF(AND(B2>=50, C2="Member"), "Gift", "")
NOT is handy for exclusions: =IF(NOT(D2="Cancelled"), B2, 0) counts revenue only for orders that weren't cancelled. The same test can be written D2<>"Cancelled", which many people find easier to read.
A common mistake is writing the test the way you'd say it in English: =IF(B2>=50 OR C2="Member", ...). Excel has no OR operator; you must use the function.
Step 5 of 11Try the calculator
Free-shipping rule tester
This mirrors =IF(OR(B2>=50, C2="Member"), 0, 7.95). Enter an order total and set Member to 1 (yes) or 0 (no). TRUE shows as 1 and FALSE as 0, just as Excel treats them in arithmetic. Try 49.99 and 50 to check the boundary.
- B2>=50 (1 = TRUE)
- 0
- OR(B2>=50, Member) (1 = TRUE)
- 0
- Shipping charged
- 7.95 $
Step 6 of 11Practice · Sort into groups
The gift rule is =IF(AND(B2>=50, C2="Member"), "Gift", ""). Which orders get a gift?
Tap an item, then tap its group (or drag it there).
Step 7 of 11Note
Step 8 of 11Learn
Choosing between AND and OR
When you translate a business rule, listen for the logic words, then check with extreme examples.
"Employees qualify for the bonus if they hit target and have at least one year of service" is AND. "Flag the invoice if it's over $10,000 or the vendor is new" is OR. Sometimes English hides the logic: "no discount for clearance items or gift cards" means the discount applies only if the item is not clearance and not a gift card:
=IF(AND(D2<>"Clearance", D2<>"Gift card"), B2*10%, 0)
Writing OR(D2<>"Clearance", D2<>"Gift card") is a classic bug: every item is either not clearance or not a gift card, so the test is always TRUE and everything gets the discount.
You can also nest them: =IF(AND(B2>=50, OR(C2="Member", E2="Promo")), 0, 7.95) gives free shipping to $50+ orders from members or promo-code users. When rules get this complex, add a short note in a cell next to the formula or, better, split the pieces into helper columns titled "Over 50?" and "Member or promo?". Each column is easy to check, and the final IF simply combines them.
Step 9 of 11Practice · What would you do?
What would you do?
Your manager says: "Approve the expense automatically if it's under $100, or if it's under $500 and has a receipt attached." Amount is in B2, and C2 says Yes or No for the receipt. Which test matches the rule?
Step 10 of 11Practice · True or false
If A1 contains 7, =OR(A1>10, A1<5) returns TRUE.
Step 11 of 11Practice · Fill in the blanks
Fill in the blanks
Complete the formula so it returns "Eligible" only when the age in B2 is at least 18 and the consent column C2 says Yes: `=IF((B2>=18, C2="Yes"), "Eligible", "Not eligible")`
Lesson complete!