A condition in code has exactly one meaning, but the sentence it came from may have two. Pick the meaning, write it with brackets, and check it with test cases.
This lesson is part of Boolean logic and representation. It pairs with creating a test table with expected outputs, since both start from the stated rule.
How do I translate a requirement?
Follow these steps in order.
- Underline each simple fact in the sentence, such as “age is under 12”.
- Give each fact a letter, for example P for under 12.
- Mark the joining words: and, or, not, but, unless.
- Write the expression, and add brackets wherever the sentence groups two facts.
- Test it on two or three chosen cases, one of which should be a boundary.
The word “but” usually means AND. The word “unless” introduces a negation: “enter unless banned” means the person may enter when NOT banned.
Worked example
Requirement: “A ticket is discounted if the buyer is under 12 or over 60, and the buyer has a member card.”
The facts are P (under 12), Q (over 60) and M (has a card). The phrase “under 12 or over 60” groups the two ages, and the card applies to the group. The condition is (P OR Q) AND M.
Test three cases.
| Buyer | P | Q | M | (P OR Q) AND M | Discount? |
|---|---|---|---|---|---|
| Age 10 with card | 1 | 0 | 1 | 1 | Yes |
| Age 65 without card | 0 | 1 | 0 | 0 | No |
| Age 30 with card | 0 | 0 | 1 | 0 | No |
The age 30 case confirms that the card alone does not earn the discount, and the age 65 case confirms that age alone does not either.
The mistake: two readings of one sentence
A common slip is to write P OR Q AND M, with no brackets. A programming language evaluates AND first, which gives P OR (Q AND M).
Test a child aged 10 without a card.
| Reading | Expression | Child aged 10, no card |
|---|---|---|
| Card applies to both ages | (P OR Q) AND M | 1 OR 0 = 1, then 1 AND 0 = 0, so no discount |
| Card applies only to over 60 | P OR (Q AND M) | P is 1, so the discount applies |
The same buyer gets opposite results, so fix one reading before you write code. If the sentence really is ambiguous, state both expressions and say which reading you chose.
Common phrases and their conditions
| Phrase | Condition |
|---|---|
| Both A and B | A AND B |
| At least one of A or B | A OR B |
| Neither A nor B | NOT A AND NOT B |
| A but not B | A AND NOT B |
| Exactly one of A or B | (A AND NOT B) OR (NOT A AND B) |
The last row is the easiest to get wrong. A plain A OR B also includes the case where both are true, which “exactly one” excludes.
Check yourself
A library lets a person borrow a DVD if they are a member and have no overdue books, or if they are a staff member. Write the condition with letters M (member), O (has overdue books) and S (staff). Test it for a member with overdue books who is not staff.
Answer
The phrase “member and no overdue books” is one group, and “staff” is an alternative. The condition is (M AND NOT O) OR S.
Test: member, has overdue books, not staff gives M = 1, O = 1, S = 0. So (1 AND NOT 1) OR 0 = (1 AND 0) OR 0 = 0 OR 0 = 0. The person cannot borrow, which matches the rule.
Without brackets, M AND NOT O OR S would evaluate AND first and give the same grouping here. The brackets are still worth writing, because they show the reading on purpose.
What to study next
The last lesson in this section separates how many bits a value uses from the value itself: distinguishing representation size from the value represented. You can check any condition in the Boolean expression and truth-table explorer.
If you want a teacher to test your conditions on tricky wording, see online one-to-one Computer Science tuition.