CIE IGCSE | 8.1.4F.2 Relational and Logical Operators

Lesson objective

Use relational and logical operators to construct, trace and explain Boolean conditions, including inclusive ranges and grouped expressions.

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.

Every relational comparison produces a BOOLEAN result
OperatorMeaningExampleResult
=Equal to7 = 7TRUE
<>Not equal to7 <> 7FALSE
<Less than7 < 7FALSE
<=Less than or equal to7 <= 7TRUE
>Greater than9 > 7TRUE
>=Greater than or equal to7 >= 7TRUE

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.

One character in the operator changes the boundary rule
ScoreScore > 50Score >= 50
49FALSEFALSE
50FALSETRUE
51TRUETRUE

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"
ENDIF

07 | 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.

NOT produces the opposite truth value
LockedNOT Locked
TRUEFALSE
FALSETRUE

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”.

All four combinations of two Boolean inputs
ABA AND BA OR B
FALSEFALSEFALSEFALSE
FALSETRUEFALSETRUE
TRUEFALSEFALSETRUE
TRUETRUETRUETRUE

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.

Inside and outside are opposite results for these numeric scores
ScoreInside: >= 0 AND <= 100Outside: < 0 OR > 100
-1FALSETRUE
0TRUEFALSE
50TRUEFALSE
100TRUEFALSE
101FALSETRUE

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"
ENDIF

15 | 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.

MATCH THE CONDITION TO ITS MEANING

Terminology

Terminology

Condition

An expression evaluated to TRUE or FALSE.

BOOLEAN

A data type with the values TRUE and FALSE.

Relational operator

An operator comparing two values and returning a Boolean result.

Operand

A value or expression on which an operator acts.

Equality (=)

Tests whether two values are equal.

Not equal (<>)

Tests whether two values differ.

Less than (<)

Tests whether the left value is strictly smaller.

Less than or equal (<=)

Tests whether the left value is smaller or equal.

Greater than (>)

Tests whether the left value is strictly larger.

Greater than or equal (>=)

Tests whether the left value is larger or equal.

Logical operator

An operator acting on Boolean operands to produce a Boolean result.

AND

TRUE only when both Boolean operands are TRUE.

OR

TRUE when at least one Boolean operand is TRUE, including both.

NOT

Reverses one Boolean value.

Truth table

A table of all possible Boolean input combinations and their results.

Inclusive boundary

An endpoint included in an accepted range.

Grouping

Using parentheses to make the intended structure of an expression clear.

Assignment (←)

Stores the evaluated right-hand-side result in the named item on the left.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Score is 50. Which conditions are TRUE?
2. Which statements about comparisons are correct?
3. A is TRUE and B is FALSE. Which are TRUE?
4. When is A OR B TRUE?
5. Which express a valid inclusive score range 0 to 100?
6. Which scores satisfy (Score < 0) OR (Score > 100)?
7. Paid TRUE, FreeEntry FALSE, SpaceAvailable FALSE. Which expressions are TRUE?
8. Which statements about NOT are correct?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Define a relational operator and a logical operator.

2. Explain why Score >= 50 differs from Score > 50.

3. Demonstrate the difference between AND and OR with one TRUE and one FALSE operand.

4. Explain why OR still gives TRUE when both inputs are TRUE.

5. Construct a condition for a temperature from -5 to 10 inclusive.

6. Construct a condition detecting a temperature outside that range.

7. Compare (Paid OR FreeEntry) AND SpaceAvailable with Paid OR (FreeEntry AND SpaceAvailable).

8. Explain the result of NOT (A AND B) when A and B are both TRUE, and state whether their stored values change.

BUILD IT | CLUB ELIGIBILITY

A club accepts ages thirteen to seventeen inclusive only when permission is recorded. Assume Age contains a valid INTEGER and Permission contains a BOOLEAN. Declare Eligible, construct the complete condition and output a suitable decision. Test ages twelve, thirteen, seventeen and eighteen with permission, then age fifteen without permission.

Reveal one possible solution
DECLARE Eligible : BOOLEAN
Eligible ← (Age >= 13) AND (Age <= 17) AND Permission
IF Eligible
  THEN
    OUTPUT "Eligible"
  ELSE
    OUTPUT "Not eligible"
ENDIF

With permission: twelve is FALSE, thirteen TRUE, seventeen TRUE and eighteen FALSE. Age fifteen without permission is FALSE. The age tests are both inclusive and all three requirements must hold. These tests assume values have already been supplied with the stated types.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 8.1.4F.2 Relational and Logical Operators is coming soon. Complete the matching task and written questions in your exercise book.