Skip to content
SPM Tuition
Computer Science · Boolean logic and representation

Turning a written requirement into a precise condition

The requirement reads clearly in words, yet your condition lets in people it should reject.

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.

  1. Underline each simple fact in the sentence, such as “age is under 12”.
  2. Give each fact a letter, for example P for under 12.
  3. Mark the joining words: and, or, not, but, unless.
  4. Write the expression, and add brackets wherever the sentence groups two facts.
  5. 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.

Common questions

How do I decide between AND and OR?

Use AND when every part must be true at the same time. Use OR when any one part is enough. Words such as both, all and as well as point to AND, while either, any and or point to OR.

What does 'neither A nor B' mean as a condition?

It means A is false and B is false, written as NOT A AND NOT B, or equivalently NOT (A OR B). A table with all four combinations confirms that only the row with both off is true.

Why are brackets needed?

Without brackets, AND is evaluated before OR, which may not match what the sentence means. Brackets state the meaning so that a reader, and a computer, can only read it one way.

What if the requirement is genuinely ambiguous?

Write both readings as separate conditions, test them on a case where they differ, and state which one you chose and why. In a real project you would check with whoever wrote the requirement.

If you are unsure which reading of a requirement is meant, one-to-one Computer Science lessons let a teacher practise asking the clarifying question and testing both readings on your examples.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.