- Foundation
- About 12 min
- 5 practice questions
Combining conditions with AND, OR and NOT
Build and evaluate conditions using AND, OR and NOT, write range checks and apply the leap-year rule.
If something looks wrong, please tell us.
A single comparison such as marks >= 40 is often not enough. Real decisions combine several conditions: is the age between 10 and 15, is the year divisible by 4 but not by 100? Logical operators join conditions together. This lesson shows how they work with truth tables and small programs.
The three logical operators
- AND: true only when both conditions are true.
- OR: true when at least one condition is true.
- NOT: reverses a condition: NOT true is false and NOT false is true.
| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
When AND and OR appear together without brackets, most languages do AND first, then OR, and NOT before both. Brackets make your meaning clear and are always a good idea.
Range checks
To test whether a value lies in a range, use two comparisons joined by AND. For ages from 10 to 15 inclusive: age >= 10 AND age <= 15. Writing age >= 10 OR age <= 15 would be true for every number, a common mistake. To test whether a value lies outside a range, use OR: age < 10 OR age > 15.
Worked example 1: evaluate
Let x = 8 and y = 3. Evaluate (x > 5) AND (y > 5). The first part is true, the second is false, so the result is False. Evaluate (x > 5) OR (y > 5): the first part is true, so the result is True. Evaluate NOT (x > 5): the inside is true, so the result is False. Note that NOT (x > 5) is the same as x <= 5.
Worked example 2: a range
A club accepts children aged 10 to 15 inclusive. The condition is age >= 10 AND age <= 15. Age 12: true AND true, so accepted. Age 9: false AND true, so rejected. Age 15: true AND true, so accepted. Age 16: true AND false, so rejected.
Worked example 3: the leap-year rule
A year is a leap year if it is divisible by 400, or if it is divisible by 4 but not by 100. In pseudocode: (year % 400 == 0) OR ((year % 4 == 0) AND (year % 100 != 0)). Test 2024: 2024 % 400 is not 0, but 2024 % 4 = 0 and 2024 % 100 = 24, so it is a leap year. Test 1900: not divisible by 400; divisible by 4 but also by 100, so not a leap year. Test 2000: divisible by 400, so a leap year. Test 2023: not divisible by 4, so not a leap year.
Worked example 4: using a condition in a decision
A shop gives a 10 percent discount if the bill is at least 1000 rupees OR the customer is a member. The pseudocode is IF (bill >= 1000) OR (member == true) THEN discount = bill * 10 / 100. For a bill of 1200 and a non-member: the first part is true, so the discount applies: 1200 x 10 / 100 = 120 rupees, and the customer pays 1200 - 120 = 1080 rupees. For a bill of 800 and a non-member, both parts are false, so there is no discount. For a bill of 800 and a member, the second part is true, so the discount applies. (The rupee amounts are illustrative.)
AND needs both; OR needs one; NOT flips. A range inside uses AND; outside uses OR.
Key takeaways
- Use brackets to avoid mistakes in mixed conditions.
- A range check joins two comparisons with AND.
- The leap-year rule combines OR, AND and a NOT-equal test.
Check your understanding
-
1. What is True AND False?
- True
- False
- Both
- Unknown
Show answer
Answer: B. AND is true only when both parts are true.
-
2. What is False OR True?
- Error
- False
- Maybe
- True
Show answer
Answer: D. OR is true when at least one part is true.
-
3. Which condition tests that a mark m is between 30 and 60 inclusive?
- m >= 30 OR m <= 60
- m > 30 OR m < 60
- m >= 30 AND m <= 60
- m <= 30 AND m >= 60
Show answer
Answer: C. A range uses two comparisons joined by AND. With OR the condition would be true for every number.
-
4. Which of these years is a leap year using the standard rule?
- 1900
- 2100
- 2016
- 2023
Show answer
Answer: C. 2016 is divisible by 4 and not by 100. 1900 and 2100 are divisible by 100 but not by 400, and 2023 is not divisible by 4.
-
5. NOT (n > 10) is the same as
- n <= 10
- n >= 10
- n < 10
- n == 10
Show answer
Answer: A. If n > 10 is false then n is 10 or less, so NOT (n > 10) means n <= 10.
Sign in to keep track of the lessons you finish.