5 / 6 · Basic
AND, OR and nested IF
A score of 81 with 70% attendance passes the score test and fails the attendance test. AND needs both; OR needs only one.
Jump to the 10 practice questionsYou will learn
- Combine tests with AND and OR
- Order nested IFs from the highest band down
- Test a rule with values on both sides of the line
Before this: The IF function in a spreadsheet
1Explain
The night before marks are due
A made-up example. A teacher builds a grade sheet the night before marks are due. The rule is A for 90 and up, B for 80, C for 70, D for 60 and F below that. She writes the tests in the order they come to mind, lowest first: =IF(B2>=60, "D", IF(B2>=70, "C", ...)). Every student with 60 or more gets a D, including one who scored 93. Nothing shows an error. A student points it out the next morning. The fix is to turn the order round, and the reason is worked out below.
2Problem
The 93 that became a D
Hint
The first test that is true wins.
Solution
Answer: D
93 ≥ 60 is TRUE at the first test, so it stops with D.
3Explain
Two conditions at once: AND and OR
The same teacher has a second rule. To pass, a student needs a score of at least 60 and attendance of at least 80%. AND takes one or more tests (Excel allows up to 255) and returns TRUE only when every one of them is TRUE: =AND(B2>=60,C2>=80). OR returns TRUE when at least one test is TRUE, so =OR(B2>=90,C2=100) could flag a student for a prize who either scored very high or never missed a class. NOT flips TRUE to FALSE and back, which suits rules phrased with "unless".
4Problem
AND with 81 and 70
B2 is 81 and C2 is 70. What does =AND(B2>=60,C2>=80) return?
Hint
Attendance is below 80.
Solution
Answer: FALSE
70 ≥ 80 is FALSE, and AND needs every test to be TRUE.
5Problem
OR with 81 and 70
Same cells. What does =OR(B2>=60,C2>=80) return?
Hint
One test is enough.
Solution
Answer: TRUE
81 ≥ 60 is TRUE, and OR needs only one.
6Problem
NOT
What does =NOT(5>3) return?
Hint
5 > 3 is TRUE.
Solution
Answer: FALSE
5 > 3 is TRUE, and NOT turns it into FALSE.
7Explain
AND and OR inside IF
On their own, AND and OR give only TRUE or FALSE. Put them inside IF to turn that into words: =IF(AND(B2>=60, C2>=80), "Yes", "No"). The tests inside can point at different columns, compare text, or use calculations. AND and OR skip text and empty cells in ranges, and if they find nothing they can test at all, they return #VALUE!. The table below runs the two tests, a score of at least 60 and attendance of at least 80, on four students.
8Problem
66 and 88
Hint
Both tests are true.
Solution
Answer: Yes
Both tests are TRUE, so AND is TRUE.
9Explore
Say the rule in words first
Say the rule in plain words before you write it. "Pass if the score is at least 60 and attendance is at least 80" maps straight onto AND; "pass if either test is passed" maps onto OR. A rule that mixes the two, such as "at least 60, and either 80% attendance or a doctor's note", needs one function inside the other: =AND(B2>=60, OR(C2>=80, D2="note")). Write the inner part first. Check that it gives TRUE and FALSE where you expect, and only then wrap it.
&& is AND and || is OR. The first line is the AND column of the table, the second the OR column.
const score = 81, attend = 70;
console.log(score >= 60 && attend >= 80);
console.log(score >= 60 || attend >= 80);false
true| Score | Attend % | AND | OR |
|---|---|---|---|
| 93 | 95 | TRUE | TRUE |
| 81 | 70 | FALSE | TRUE |
| 66 | 88 | TRUE | TRUE |
| 55 | 70 | FALSE | FALSE |
10Explain
Why the 93 in the story became a D
IF gives two outcomes. For more, put another IF in the place of the false answer, and the grade scale becomes =IF(B2>=90, "A", IF(B2>=80, "B", IF(B2>=70, "C", IF(B2>=60, "D", "F")))). The spreadsheet tries the first test and moves to the next only when it fails. That is exactly what went wrong in the story. With B2>=60 first, a 93 passes the very first test and stops at D. The higher tests are never read. Start with the highest threshold and work down.
11Problem
93 in the right order
Hint
The first test is >= 90.
Solution
Answer: A
The first test is TRUE, so the inner IF is never reached.
12Problem
85 in the right order
Hint
Not 90, but at least 80.
Solution
Answer: B
85 ≥ 90 is FALSE, then 85 ≥ 80 is TRUE.
13Explain
Brackets and IFS
Count the brackets: four IFs need four closing brackets at the end. Excel's limits page allows 64 nested levels of functions, but beyond three or four levels a formula gets hard to read and easy to break. Newer versions of both programs have IFS, which lists test-and-answer pairs one after another without nesting, and Google's IF help links to it. For a long scale the lookup table in the next unit is usually clearer still, because the thresholds sit in cells where anyone can see and change them.
14Problem
Closing brackets
How many closing brackets end a formula with four nested IFs and nothing else?
Hint
One for each IF.
Solution
Answer: 4
Each IF opens one bracket that must be closed.
15Problem
Nested levels
How many nested levels of functions does Excel allow?
Hint
Excel's limits page.
Solution
Answer: 64
Excel's specifications and limits page: 64.
16Explore
Practice in the grid
In the lab below, the grade column uses four nested IFs and the pass column uses IF with AND. Fill both down and compare Ben with Dev. Ben has the better score and fails on attendance. Dev turns up but scores below 60.
Drag to turn. Scroll or pinch to zoom.
17Explain
One true, one false
Test a combined rule with one condition true and the other false, then swap which condition fails. AND must reject both cases, while OR must accept both. This distinguishes the functions more effectively than testing only a case where everything passes. For a nested rule, also check an input that reaches the inner test rather than stopping at the outer one.
18Problem
Good test values
Which scores best test a "60 or more passes" formula?
Hint
Right at the line and just below it.
Solution
Answer: 60 and 59.9
Mistakes hide at the boundary, where > and >= differ.
19Practice
Nested IFs in the right order
Finish grade(score) for A from 90, B from 80, C from 70, D from 60 and F below. Test the highest band first.
A
B
C
D
Ffunction grade(score) {
return "F";
}
for (const s of [93, 85, 70, 60, 59.9]) console.log(grade(s));
Hint
Once a test is true the function returns, so the order decides.
A solution
function grade(score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
if (score >= 60) return "D";
return "F";
}
for (const s of [93, 85, 70, 60, 59.9]) console.log(grade(s));20Explain
Common mistakes
Starting a nested IF with the lowest threshold.
The first TRUE test wins. With B2>=60 first, every score of 60 or more stops at D.
Writing =IF(B2>=60 AND C2>=80, ...).
AND is a function that takes the tests as arguments, not a word placed between them. Write AND(B2>=60, C2>=80).
Using OR when the rule says both.
A student with 81 points and 70% attendance passes with OR and fails with AND. Say the rule aloud: an "and" in the sentence means AND.
Losing count of the closing brackets.
Four nested IFs need four ) at the end. With one missing, the formula is not accepted.
21
Summary
- AND: every test TRUE. OR: at least one TRUE. NOT: the opposite.
- Use them inside IF to turn TRUE or FALSE into words.
- Nested IF: put the next IF in the false slot, highest threshold first.
- Test the boundaries. For long scales, a lookup table is clearer.