In logic, AND is true only when both parts are true, and OR is true when at least one part is true. In everyday language the same words can mean something looser, which is why a sentence must be turned into brackets before it becomes a condition.
This lesson uses the operators from evaluating expressions and the case checks from comparing expressions. It closes the teaching lessons of the Boolean logic module.
Where does ordinary language mislead?
There are three common traps.
- “And” used to list groups. “Free entry for children under 5 and adults over 65” lists two groups. One person is never in both, so the condition needs OR.
- “Or” meaning one or the other. “You may have tea or coffee” usually excludes both. Logical OR allows both, and XOR excludes both.
- Mixed AND and OR with no brackets. “The door is open and the system is not armed or the panic button is pressed” has two readings.
How to translate a sentence
- Name each simple statement with a letter.
- Mark the operators in the sentence: and, or, not.
- Ask what is being grouped and write brackets.
- Test a case where different readings would disagree.
Worked example 1
Rule: “Free entry for visitors under 5 and over 65.”
Wrong translation: IF (Age < 5) AND (Age > 65) THEN. Test Age = 3: (3 < 5) is true and (3 > 65) is false, so true AND false = false. A three-year-old would be refused.
Correct: IF (Age < 5) OR (Age > 65) THEN. Test Age = 3: true OR false = true.
Test Age = 30: false OR false = false. Test Age = 70: false OR true = true.
Worked example 2
Rule: “The alarm sounds if the door is open and the system is not armed or the panic button is pressed.”
Let D = door open, S = system armed, P = panic button pressed.
- Reading 1: (D AND NOT S) OR P
- Reading 2: D AND (NOT S OR P)
Test D = 0, S = 0, P = 1 (door closed, system off, panic pressed).
Reading 1: (0 AND 1) OR 1 = 0 OR 1 = 1. Reading 2: 0 AND (1 OR 1) = 0 AND 1 = 0.
The readings give different results, so the sentence is ambiguous. Reading 1 sounds the alarm whenever panic is pressed, which is probably the intention, but only the person writing the rule can say. In a program you must choose, and brackets record the choice:
IF (Door = TRUE AND Armed = FALSE) OR Panic = TRUE THEN
OUTPUT "ALARM"
ENDIF
The mistake to watch for
Mistaken working: “The rule says ‘and’, so I will use AND everywhere.”
The word in the sentence is not always the operator the condition needs. Decide from the meaning, then check with a case. A good habit is to test one input that should clearly pass and one that should clearly fail.
Check yourself
1. “Visitors may enter if they have a ticket and a pass or are staff.” Write two bracketed readings.
Show answer
Let T = ticket, P = pass, S = staff. Reading 1: (T AND P) OR S. Reading 2: T AND (P OR S). Test T = 0, P = 0, S = 1: reading 1 gives 1, reading 2 gives 0, so they differ.
2. “Not hot and humid.” Give two readings and a case where they differ.
Show answer
Let H = hot, U = humid. Reading 1: NOT (H AND U). Reading 2: (NOT H) AND U. Test H = 1, U = 0: reading 1 gives NOT 0 = 1. Reading 2 gives 0 AND 0 = 0. They differ.
3. A menu says “choose soup or salad”. Which gate matches the intended meaning, if you may not take both?
Show answer
XOR, because it is 1 only when exactly one choice is selected. OR would also accept both.
Where this leads next
Finish the module with the Boolean logic practice set, which mixes all five skills. The safe Python reasoning sandbox lets you test conditions such as the alarm rule safely.
Turning words into precise conditions is a skill our teachers coach one example at a time in online one-to-one Computer Science tuition.