Excel IF Function and Logical Tests
The IF function lets Excel make a decision: if something is true, show one result; otherwise show another.
The IF Function
Example
=IF(logical_test, value_if_true, value_if_false)- logical_test: a question with a yes/no answer, like
B2>=50 - value_if_true: what to show if the answer is yes
- value_if_false: what to show if the answer is no
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | A2="Karachi" |
<> | Not equal to | A2<>"Karachi" |
> | Greater than | B2>50 |
< | Less than | B2<50 |
>= | Greater than or equal to | B2>=50 |
<= | Less than or equal to | B2<=50 |
Pass or Fail
Students pass with 50 marks or more. In C2:
Example
=IF(B2>=50, "Pass", "Fail")| A | B | C | |
|---|---|---|---|
| 1 | Student | Marks | Result |
| 2 | Ali | 78 | Pass |
| 3 | Bilal | 42 | Fail |
| 4 | Ayesha | 91 | Pass |
Text in a formula must be inside double quotes, like "Pass". Numbers do not need quotes.
IF with a Calculation
The result can also be a calculation. A shop gives 5% discount on bills of Rs 5000 or more:
Example
=IF(B2>=5000, B2*5%, 0)| A | B | C | |
|---|---|---|---|
| 1 | Customer | Bill | Discount |
| 2 | Sana | 7200 | 360 |
| 3 | Hamza | 3100 | 0 |
AND and OR
Use AND when all conditions must be true, and OR when at least one must be true.
A scholarship needs marks of 80 or more and attendance of 90% or more:
Example
=IF(AND(B2>=80, C2>=90%), "Eligible", "Not eligible")Free delivery is given if the bill is over Rs 3000 or the customer is a member:
Example
=IF(OR(B2>3000, C2="Yes"), "Free", "Rs 200")| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer | Bill | Member | Delivery |
| 2 | Ali | 3500 | No | Free |
| 3 | Ayesha | 1800 | Yes | Free |
| 4 | Bilal | 1200 | No | Rs 200 |
Nested IF
To choose between more than two results, put an IF inside another IF. Grades from marks:
Example
=IF(B2>=80, "A", IF(B2>=70, "B", IF(B2>=60, "C", "F")))Excel checks from left to right and stops at the first true test.
IFS: A Cleaner Way
In Excel 2019, Excel 2021 and Microsoft 365, IFS does the same thing more clearly:
Example
=IFS(B2>=80, "A", B2>=70, "B", B2>=60, "C", TRUE, "F")The last pair TRUE, "F" works as "everything else".
| Marks | Grade |
|---|---|
| 91 | A |
| 74 | B |
| 63 | C |
| 42 | F |
IFERROR
IFERROR shows something friendly instead of an error:
Example
=IFERROR(B2/C2, 0)
=IFERROR(B2/C2, "No data")| B | C | =IFERROR(B2/C2, "No data") |
|---|---|---|
| 500 | 10 | 50 |
| 500 | 0 | No data |
Use IFERROR carefully. It hides all errors, including real mistakes in your formula.
Exercise
Exercise
Cell B2 contains 45. What does =IF(B2>=50, "Pass", "Fail") show?
