A-Level Computer Science / Unit 11: Structured Programming

11.1.3 Arithmetic, Comparison and Logical Expressions

πŸ”’ Lesson slides are available to signed-in users. Sign in

11.1.3 Arithmetic, Comparison and Logical Expressions

An expression combines values, identifiers and operators to produce a result. The result may be numeric, textual or Boolean, depending on the values and operators used.

This section concentrates on three expression types that are essential in pseudocode: arithmetic expressions, comparisons and logical expressions.

By the end of this section, you should be able to:

  • Recognise the result produced by an expression.
  • Use arithmetic operators, including DIV and MOD.
  • Apply precedence rules and brackets correctly.
  • Construct comparisons using the appropriate operator.
  • Combine Boolean conditions using AND, OR and NOT.
  • Evaluate a compound expression in a reliable order.
  • Store the result of an expression using assignment.
  • Distinguish assignment from equality comparison.
Connection to nearby pages: variables and assignment were introduced in 11.1.2. This page focuses on the expression evaluated before a result is assigned, output or used as a condition.

What Is an Expression?

An expression is a combination of one or more values that can be evaluated to produce a single result.

Expression: a combination of literals, identifiers, function calls and operators that produces a value.
Expression Type of result Example result
ItemsPacked + ItemsWaiting Numeric 47
Temperature > 30 Boolean TRUE
FirstName & " " & FamilyName String A combined name

This page focuses on numeric and Boolean results. String manipulation and supplied library routines are developed in 11.1.4.

Common misconception: an expression is not necessarily a complete statement. For example, ItemsPacked + 5 produces a value, but it does not store or output that value by itself.

Arithmetic Expressions

Arithmetic expressions use numeric values and operators to calculate a numeric result.

Operation Pseudocode operator Example
Addition + Completed + Waiting
Subtraction - Capacity - Occupied
Multiplication * Length * Width
Real division / TotalDistance / JourneyCount
Exponentiation ^ SideLength ^ 2
Integer division DIV DeviceCount DIV DevicesPerCase
Remainder MOD DeviceCount MOD DevicesPerCase

Storing a calculated result

DECLARE AvailableSeats : INTEGER

AvailableSeats ← SeatCapacity - SeatsReserved

The expression on the right is evaluated first. Its result is then assigned to AvailableSeats.

Read an assignment from right to left: calculate the expression, then store the result in the identifier on the left.

Real Division, Integer Division and Remainders

The three division-related operators answer different questions.

Expression Question answered Result
29 / 6 What is the numerical quotient? Approximately 4.8333
29 DIV 6 How many complete groups of six are possible? 4
29 MOD 6 How many items remain after making complete groups? 5

Original scenario: packing devices

A shipping case holds six devices. There are 29 devices waiting to be packed.

FullCases ← 29 DIV 6
DevicesRemaining ← 29 MOD 6

The result is four full cases and five devices remaining.

Common mistake: MOD does not calculate a percentage and does not return the whole-number quotient. It returns the remainder.

Operator Precedence

When an expression contains several operators, precedence rules determine which operations are evaluated first.

Priority Operation Examples
1 Brackets ( ... )
2 Exponentiation ^
3 Multiplication and division *, /, DIV, MOD
4 Addition and subtraction +, -

Operators at the same precedence level are normally evaluated from left to right unless brackets specify a different order.

Worked evaluation

18 + 4 * 3 ^ 2
Stage Expression Reason
Start 18 + 4 * 3 ^ 2 Original expression
1 18 + 4 * 9 Exponentiation first
2 18 + 36 Multiplication next
3 54 Addition last

Brackets change the result

(18 + 4) * 3 ^ 2
22 * 9
198
Use brackets when they make your intended grouping clearer, even when the default precedence would produce the same result.

Comparison Expressions

A comparison examines the relationship between two values. Its result is always Boolean: TRUE or FALSE.

Comparison expression: an expression that compares two values and produces a Boolean result.
Relationship Pseudocode operator Example
Equal to = Zone = 'B'
Not equal to <> Status <> "Closed"
Greater than > Reading > AlertLimit
Less than < StockLevel < MinimumStock
Greater than or equal to >= Age >= 16
Less than or equal to <= Temperature <= MaximumSafe

Example evaluation

Suppose Reading stores 72.

Expression Result Reason
Reading > 65 TRUE 72 is greater than 65
Reading = 65 FALSE 72 and 65 are not equal
Reading <= 72 TRUE 72 is equal to the upper limit
Assignment and equality are different: Reading ← 72 stores a value, while Reading = 72 tests whether the current value is 72.

Logical Operators

Logical operators combine or reverse Boolean values. They allow a condition to represent several requirements.

Operator Result is TRUE when... Example
AND Both conditions are true HasPass AND IsRegistered
OR At least one condition is true IsSupervisor OR HasTemporaryAccess
NOT The original condition is false NOT IsDoorLocked

AND truth table

Condition A Condition B A AND B
TRUETRUETRUE
TRUEFALSEFALSE
FALSETRUEFALSE
FALSEFALSEFALSE

OR truth table

Condition A Condition B A OR B
TRUETRUETRUE
TRUEFALSETRUE
FALSETRUETRUE
FALSEFALSEFALSE

NOT truth table

Condition NOT Condition
TRUEFALSE
FALSETRUE
Common misconception: Boolean OR is inclusive. It is true when one condition is true and also when both conditions are true.

Evaluating Combined Expressions

A compound expression may include arithmetic operations, comparisons and logical operators in the same statement.

A reliable evaluation order

  1. Evaluate bracketed expressions.
  2. Complete arithmetic calculations.
  3. Evaluate each comparison.
  4. Apply NOT.
  5. Apply AND.
  6. Apply OR.

Brackets should be used when the intended logical grouping might otherwise be unclear.

Testing whether a value lies within a range

(ParticipantAge >= 13) AND (ParticipantAge <= 17)

Both comparisons must be true, so AND is appropriate.

Avoid writing a mathematical chained comparison such as 13 <= ParticipantAge <= 17. Write two complete comparisons joined by AND.

Storing a Boolean result

DECLARE CanEnter : BOOLEAN

CanEnter ← (ParticipantAge >= 13) AND HasEntryPass

The comparison and logical operation are evaluated first. The resulting Boolean value is then stored in CanEnter.

Worked Example: Environmental Alert

A monitoring station adjusts a sensor reading before deciding whether an alert condition exists.

AdjustedReading ← RawReading - CalibrationOffset

IsAlert ← (AdjustedReading > AlertLimit)
          AND NOT IsMaintenanceMode

Use these values:

Identifier Value
RawReading 86
CalibrationOffset 4
AlertLimit 80
IsMaintenanceMode FALSE

Step 1: evaluate the arithmetic expression

AdjustedReading ← 86 - 4
AdjustedReading ← 82

Step 2: evaluate the comparison

AdjustedReading > AlertLimit
82 > 80
TRUE

Step 3: apply NOT

NOT IsMaintenanceMode
NOT FALSE
TRUE

Step 4: apply AND

TRUE AND TRUE
TRUE

Therefore, IsAlert receives the value TRUE.

For a compound expression, show the result of each subexpression before giving the final Boolean result.

Interactive: Arithmetic Expression Tracer

Use the existing tracer to follow arithmetic statements step by step. The ticket and quiz examples show calculated results, while the integer-division example contrasts quotient and remainder operations.

Ticket total

Program state

Console output --

Step 1 of 5

Declare variables

The algorithm prepares the values required by the calculation.

Focus on the expression being evaluated at each step rather than the input and output syntax surrounding it.

Interactive: Boolean Expression Evaluator

Change the values and operators to see how two comparisons are evaluated and then joined using AND or OR. You can also apply NOT to the final result.

Values

Comparisons

score
attendance

score >= 50 AND attendance >= 80

Left comparison True 65 >= 50
Right comparison True 82 >= 80
Final result True True AND True gives True
Evaluate the two comparisons independently before applying the logical operator.

Common Mistakes and Misconceptions

  • Working only from left to right: ignoring arithmetic precedence.
  • Confusing division operators: using /, DIV and MOD as though they produce the same result.
  • Confusing assignment and equality: using ← to test equality or = to assign a value.
  • Forgetting inclusive limits: translating β€œat least” as > instead of >=.
  • Using AND for alternatives: writing Zone = 'A' AND Zone = 'B' when either value is accepted.
  • Misunderstanding OR: assuming it becomes false when both conditions are true.
  • Applying NOT to the wrong part: failing to use brackets to show which condition is reversed.
  • Writing chained comparisons: using mathematical notation instead of two complete comparisons joined by AND.

Practice

Question 1: arithmetic operators

Evaluate:

  1. 26 + 8 * 3
  2. (26 + 8) * 3
  3. 5 ^ 2 + 7

Question 2: complete groups and remainder

A container holds seven components. There are 46 components. State the results of:

  1. 46 DIV 7
  2. 46 MOD 7

Question 3: comparisons

Suppose BatteryLevel stores 40.

  1. BatteryLevel > 40
  2. BatteryLevel >= 40
  3. BatteryLevel <> 15

Question 4: logical expressions

Use:

HasPass = TRUE
IsRegistered = FALSE
IsSupervisor = TRUE

Evaluate:

  1. HasPass AND IsRegistered
  2. HasPass OR IsRegistered
  3. NOT IsRegistered
  4. IsRegistered OR IsSupervisor

Question 5: range condition

Write a Boolean expression that is true when Humidity is between 35 and 60 inclusive.

Question 6: compound evaluation

Use:

StockLevel = 31
ReservedStock = 8
MinimumAvailable = 12
IsWarehouseClosed = FALSE

Evaluate:

(StockLevel - ReservedStock >= MinimumAvailable)
AND NOT IsWarehouseClosed

Question 7: correct the expression

A value is valid when Code is either 'K' or 'M'.

Code = 'K' AND Code = 'M'

Explain the error and write the corrected expression.

Show suggested answers

Question 1

  1. 26 + 8 * 3 = 26 + 24 = 50
  2. (26 + 8) * 3 = 34 * 3 = 102
  3. 5 ^ 2 + 7 = 25 + 7 = 32

Question 2

  • 46 DIV 7 = 6
  • 46 MOD 7 = 4

Question 3

  • BatteryLevel > 40 is FALSE.
  • BatteryLevel >= 40 is TRUE.
  • BatteryLevel <> 15 is TRUE.

Question 4

  1. FALSE
  2. TRUE
  3. TRUE
  4. TRUE

Question 5

(Humidity >= 35) AND (Humidity <= 60)

Question 6

  1. 31 - 8 = 23
  2. 23 >= 12 is TRUE
  3. NOT FALSE is TRUE
  4. TRUE AND TRUE is TRUE

Question 7

One character cannot equal both values at the same time. The alternatives must be joined using OR.

(Code = 'K') OR (Code = 'M')

Review

Concept Key idea Example
Arithmetic expression Produces a numeric result Capacity - Occupied
DIV Returns the complete whole-number groups 29 DIV 6 = 4
MOD Returns the remainder 29 MOD 6 = 5
Comparison Produces TRUE or FALSE Reading > Limit
AND Both conditions must be true HasPass AND IsRegistered
OR At least one condition must be true IsStaff OR HasPermit
NOT Reverses a Boolean result NOT IsClosed
Final exam tip: separate a complex expression into smaller parts. Evaluate arithmetic first, then comparisons, then logical operators, recording each intermediate result.