Learn
8.1.4F.2 | RELATIONAL AND LOGICAL OPERATORS
01 | ASK A QUESTION THE PROGRAM CAN ANSWER
A sports club accepts members aged thirteen or older. A program can store an age, but that number alone does not decide whether the person meets the rule. It must compare the age with thirteen.
A condition is an expression evaluated as TRUE or FALSE. TRUE means the test is satisfied; FALSE means it is not. These are the two values of the BOOLEAN data type. Neither answer is an error: each tells the program which situation it is dealing with.
With Age equal to fourteen, Age >= 13 is TRUE. With Age equal to twelve, it is FALSE. The comparison does not change Age. It produces a Boolean result that another instruction can use.
02 | RELATIONAL OPERATORS COMPARE VALUES
A relational operator compares two values. The values on either side are its operands. In Age >= 13, Age and thirteen are the operands; greater than or equal to is the operator.
Use these six symbols in Cambridge pseudocode. Pay attention to the equality part of an operator because it decides whether an exact boundary is accepted.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| = | Equal to | 7 = 7 | TRUE |
| <> | Not equal to | 7 <> 7 | FALSE |
| < | Less than | 7 < 7 | FALSE |
| <= | Less than or equal to | 7 <= 7 | TRUE |
| > | Greater than | 9 > 7 | TRUE |
| >= | Greater than or equal to | 7 >= 7 | TRUE |
03 | TESTING EQUALITY IS NOT ASSIGNMENT
The assignment arrow stores a value. The equality operator tests whether two values are equal. The result of a test can itself be stored in a BOOLEAN variable.
Here Score receives fifty, then Passed receives TRUE because the comparison is true. Score remains fifty. Writing Passed ← Score >= 50 means evaluate the right side first, then store its Boolean result on the left.
DECLARE Score : INTEGER DECLARE Passed : BOOLEAN Score ← 50 Passed ← Score >= 50 OUTPUT Passed
04 | CHECK THE EXACT BOUNDARY
“At least fifty” includes fifty, so use greater than or equal to. “More than fifty” excludes fifty, so use greater than. Translate the wording before choosing a symbol.
Checking forty-nine, fifty and fifty-one exposes a common mistake: the two conditions agree away from the boundary but disagree at fifty. This is why boundary test data is so valuable.
| Score | Score > 50 | Score >= 50 |
|---|---|---|
| 49 | FALSE | FALSE |
| 50 | FALSE | TRUE |
| 51 | TRUE | TRUE |
05 | LOGICAL OPERATORS COMBINE BOOLEAN RESULTS
A club might require both a suitable age and permission from a parent. One comparison cannot express both requirements. Logical operators combine or change Boolean values: AND, OR and NOT.
The relational tests are evaluated to Boolean results, which are then used by the logical operator. (Age >= 13) AND Permission combines the comparison result with the BOOLEAN variable Permission. Do not put an unexplained number into an AND expression: its operands must be Boolean.
Parentheses group an expression so that its intended meaning is clear. They are especially useful when several operators appear together.
06 | AND MEANS BOTH REQUIREMENTS
A door opens only when a valid pass is present and the building is open. Either condition failing prevents entry. AND is TRUE only when both operands are TRUE.
If HasPass is TRUE but BuildingOpen is FALSE, the person has a pass but cannot enter yet. The combined condition is FALSE. If both are TRUE, the condition is TRUE and the entry message is output.
IF HasPass AND BuildingOpen
THEN
OUTPUT "Entry allowed"
ELSE
OUTPUT "Entry refused"
ENDIF07 | OR MEANS AT LEAST ONE, INCLUDING BOTH
An event admits someone who has paid or has a free-entry pass. Either route is enough. OR is TRUE when at least one operand is TRUE, including when both are TRUE.
This is inclusive OR. Everyday wording sometimes uses “or” to suggest choosing only one thing, but the pseudocode operator does not exclude the case where both are true. Someone who has paid and also holds a pass still satisfies Paid OR FreeEntry.
OR becomes FALSE only when both operands are FALSE.
08 | NOT REVERSES ONE BOOLEAN VALUE
NOT takes one Boolean operand and reverses it: TRUE becomes FALSE, and FALSE becomes TRUE. It does not change the stored operand unless you assign the result back.
If Locked is TRUE, NOT Locked is FALSE. The program is asking whether the door is not locked; it has not unlocked the door. To change the variable you would need an assignment, such as Locked ← FALSE.
| Locked | NOT Locked |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
09 | READ A TRUTH TABLE
A truth table lists every possible combination of Boolean inputs and the result of an expression. A and B below stand for two Boolean values, not numbers or particular club rules.
There are four combinations because each input can be TRUE or FALSE. AND has one TRUE row: both true. OR has one FALSE row: both false. Comparing these columns is more reliable than memorising a vague phrase such as “either condition”.
| A | B | A AND B | A OR B |
|---|---|---|---|
| FALSE | FALSE | FALSE | FALSE |
| FALSE | TRUE | FALSE | TRUE |
| TRUE | FALSE | FALSE | TRUE |
| TRUE | TRUE | TRUE | TRUE |
10 | INSIDE A RANGE USE AND
A valid score must be between zero and one hundred, including both endpoints. It must satisfy the lower limit and the upper limit together.
A score of minus five fails the lower test even though it passes the upper test. One hundred and five passes the lower test but fails the upper test. AND rejects both. Zero and one hundred each satisfy both comparisons, so they are accepted.
Avoid 0 <= Score <= 100 in these Cambridge conditions. Write the two complete comparisons and join their Boolean results.
Valid ← (Score >= 0) AND (Score <= 100)
11 | OUTSIDE A RANGE USE OR
An invalid score is below zero or above one hundred. Either fault is enough to reject it, so the outside-range condition uses OR.
Using (Score < 0) AND (Score > 100) would be a mistake. A single score cannot be both below zero and above one hundred, so that condition never identifies an out-of-range score.
The outside condition can also be written NOT ((Score >= 0) AND (Score <= 100)). NOT reverses the result of the whole valid-range test. In both versions, assume Score already holds a number; a range check does not prove that raw text input is numeric.
| Score | Inside: >= 0 AND <= 100 | Outside: < 0 OR > 100 |
|---|---|---|
| -1 | FALSE | TRUE |
| 0 | TRUE | FALSE |
| 50 | TRUE | FALSE |
| 100 | TRUE | FALSE |
| 101 | FALSE | TRUE |
12 | GROUP MIXED LOGIC TO MATCH THE RULE
An event requires a payment or a free-entry pass, and it must have a space available. First group the two acceptable entry routes. Then require that grouped result and a space.
Suppose Paid is TRUE, FreeEntry is FALSE and SpaceAvailable is FALSE. The bracketed part is TRUE, but TRUE AND FALSE is FALSE, so entry is refused.
Paid OR (FreeEntry AND SpaceAvailable) expresses a different rule: payment alone would allow entry even when there is no space. Parentheses communicate which requirements belong together.
Similarly, NOT (Paid OR FreeEntry) asks whether neither route is satisfied. (NOT Paid) OR FreeEntry asks a different question. NOT applies to its operand, so the grouping matters.
Allowed ← (Paid OR FreeEntry) AND SpaceAvailable
13 | TRY EVERY ENTRY COMBINATION
Change the three Boolean inputs below. A checked box means TRUE; an unchecked box means FALSE. Predict the grouped result before reading the output. In particular, try both entry routes with no available space.
The demo evaluates the same rule each time. It changes the inputs, then calculates fresh Boolean results. Reading a condition does not itself reserve a space or collect a payment.
14 | FOLLOW A COMPLETE MEMBERSHIP DECISION
A junior club accepts ages thirteen to seventeen inclusive, provided permission has been recorded. The example below uses known values so you can concentrate on the condition rather than input handling.
Age fifteen satisfies both limits, but Permission is FALSE. The final AND therefore gives FALSE and the output is “Not eligible”. Change Permission to TRUE and the output becomes “Eligible”. Age eighteen remains ineligible even with permission.
A Boolean variable can appear directly in a condition. AND Permission already asks whether Permission is TRUE; comparing it with TRUE is unnecessary.
DECLARE Age : INTEGER
DECLARE Permission : BOOLEAN
DECLARE Eligible : BOOLEAN
Age ← 15
Permission ← FALSE
Eligible ← (Age >= 13) AND (Age <= 17) AND Permission
IF Eligible
THEN
OUTPUT "Eligible"
ELSE
OUTPUT "Not eligible"
ENDIF15 | BUILD AND EXPLAIN A CONDITION
Start with a plain-language rule. Separate its individual tests, decide whether all or any are required, and group related tests with parentheses. Check exact boundaries and every relevant Boolean combination.
A strong explanation connects the result to the rule: “The score is accepted because it satisfies both limits, therefore the AND expression is TRUE.” For OR, explain which route is enough and remember that both may be true.
Do not assume a logical expression safely guards every later operation. For example, an expression containing division by a possibly zero value should not rely on an assumed short-circuit evaluation rule. Test the divisor first using selection before carrying out the division.
The next lesson uses nested selection to make a decision within another decision. The conditions still produce the same two Boolean results.