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
DIVandMOD. - Apply precedence rules and brackets correctly.
- Construct comparisons using the appropriate operator.
- Combine Boolean conditions using
AND,ORandNOT. - Evaluate a compound expression in a reliable order.
- Store the result of an expression using assignment.
- Distinguish assignment from equality comparison.
What Is an Expression?
An expression is a combination of one or more values that can be evaluated to produce a single result.
| 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.
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.
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.
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
Comparison Expressions
A comparison examines the relationship between two values. Its result is
always Boolean: TRUE or FALSE.
| 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 |
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 |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE |
| FALSE | TRUE | FALSE |
| FALSE | FALSE | FALSE |
OR truth table
| Condition A | Condition B | A OR B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | TRUE |
| FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE |
NOT truth table
| Condition | NOT Condition |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
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
- Evaluate bracketed expressions.
- Complete arithmetic calculations.
- Evaluate each comparison.
- Apply
NOT. - Apply
AND. - 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.
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.
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.
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.
Common Mistakes and Misconceptions
- Working only from left to right: ignoring arithmetic precedence.
-
Confusing division operators: using
/,DIVandMODas 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:
26 + 8 * 3(26 + 8) * 35 ^ 2 + 7
Question 2: complete groups and remainder
A container holds seven components. There are 46 components. State the results of:
46 DIV 746 MOD 7
Question 3: comparisons
Suppose BatteryLevel stores 40.
BatteryLevel > 40BatteryLevel >= 40BatteryLevel <> 15
Question 4: logical expressions
Use:
HasPass = TRUE
IsRegistered = FALSE
IsSupervisor = TRUE
Evaluate:
HasPass AND IsRegisteredHasPass OR IsRegisteredNOT IsRegisteredIsRegistered 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
-
26 + 8 * 3 = 26 + 24 = 50 -
(26 + 8) * 3 = 34 * 3 = 102 -
5 ^ 2 + 7 = 25 + 7 = 32
Question 2
46 DIV 7 = 646 MOD 7 = 4
Question 3
BatteryLevel > 40isFALSE.BatteryLevel >= 40isTRUE.BatteryLevel <> 15isTRUE.
Question 4
FALSETRUETRUETRUE
Question 5
(Humidity >= 35) AND (Humidity <= 60)
Question 6
31 - 8 = 2323 >= 12isTRUENOT FALSEisTRUETRUE AND TRUEisTRUE
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 |