Write pseudocode statements for: expressions involving any of the arithmetic or logical operators input from the keyboard and output to the console

11.1 Programming Basics

Learning Objectives

  • Read arithmetic or logical expressions from the keyboard.
  • Evaluate the expressions using the correct operators, functions and precedence rules.
  • Display the result on the console.
  • Write simple selection (IF‑THEN‑ELSE) and loop (FOR/WHILE) structures when required.

Pseudocode Syntax Conventions (Cambridge style)

  • Input: READ variable – prompts the user and stores the value. Only READ (or READ …  with a prompt) is accepted in the exam.
  • Output: WRITE expression – displays the evaluated expression. Only WRITE is accepted.
  • Assignment: variable ← expression
  • Selection: IF condition THEN … END IF (or ELSE branch when required)
  • Looping: FOR i ← start TO end DO … END FOR or WHILE condition DO … END WHILE
  • Comments: # comment text
  • Keywords (READ, WRITE, IF, THEN, END IF, FOR, WHILE, etc.) are case‑insensitive; the syllabus recommends writing them in UPPER‑CASE for clarity.
  • Indentation and a single space around operators improve readability and are expected in exam scripts.

Variable & Constant Declaration (optional)

Explicit declarations are not required in the exam, but they help the examiner see the intended data type.

# Declaration (optional)

INTEGER a, b, n, i

REAL total, average, C, F

CONST PI ← 3.14159

Constants are introduced with CONST and never altered after the initial assignment.

Operator Summary

CategoryOperator(s)PurposeExample
Arithmetic+Additiona + b
Arithmetic-Subtractiona - b
Arithmetic*Multiplicationa * b
Arithmetic/Real division (INTEGER ÷ INTEGER → REAL)a / b
ArithmeticmodRemainder after integer divisiona mod b
Relational=, <>, <, >, ≤, ≥Comparison (produces TRUE/FALSE)a ≥ b
LogicalANDBoth conditions true(x AND y)
LogicalORAt least one condition true(x OR y)
LogicalNOTNegates a conditionNOT x

Quick Reference – Operator Precedence (exam tip)

Order (highest → lowest)
1. Parentheses ( )
2. Unary NOT
3. Multiplication *, Division /, Modulo mod
4. Addition +, Subtraction -
5. Relational =, <>, <, >, ≤, ≥
6. Logical AND
7. Logical OR

When in doubt, enclose the intended part of an expression in parentheses – this avoids ambiguity and loss of marks.

Type‑Conversion Notes

  • Division of two INTEGER values yields a REAL result (e.g., 5 / 2 = 2.5).
  • To obtain an integer result, wrap the expression with INT() (truncates toward zero).
  • Mixed‑type arithmetic (INTEGER + REAL) is automatically promoted to REAL.
  • Be aware of overflow/under‑flow when converting a very large REAL to INTEGER – the result is undefined in the exam environment.

Built‑in Functions (syllabus‑approved only)

FunctionPurposeSyntax example
ABS(x)Absolute valueabsVal ← ABS(-7)
SQRT(x)Square root (real result)root ← SQRT(25)
INT(x)Truncate to integer (floor)i ← INT(3.9) # i = 3
POWER(x, y)x raised to the power yp ← POWER(2, 5) # p = 32
LEN(s)Length of a stringl ← LEN("Cambridge")

Only the functions listed above are guaranteed to be available in the Cambridge exam environment.

Common Pitfalls

  • Using = for assignment – in pseudocode = is a relational operator; assignment uses the left‑arrow .
  • Forgetting parentheses when mixing operators of different precedence.
  • Assuming integer division – remember that / always produces a real result.
  • Mixing data types without considering promotion (e.g., adding an INTEGER to a REAL automatically yields a REAL).
  • Omitting the CONST keyword when defining a constant.
  • Using functions not listed in the syllabus (e.g., LOG, EXP) – these are not permitted in the exam.

Worked Examples (Cambridge‑style)

Example 1 – Simple Arithmetic (no function)

# Sum, product and remainder of two integers

READ a

READ b

sum ← a + b

product ← a * b

remainder ← a mod b

WRITE "Sum = " + sum

WRITE "Product = " + product

WRITE "Remainder = " + remainder

Example 2 – Logical test with relational operators

# Eligibility based on age and test score

READ age

READ score

eligible ← (age ≥ 18) AND (score ≥ 70)

WRITE "Eligibility: " + eligible

Example 3 – Selection (IF‑THEN‑ELSE) with a relational test

# Pass/fail decision based on average mark

READ m1

READ m2

READ m3

average ← (m1 + m2 + m3) / 3 # real division

IF average ≥ 50 THEN

result ← "PASS"

ELSE

result ← "FAIL"

END IF

WRITE "Average = " + average

WRITE "Result = " + result

Example 4 – Loop – factorial using a FOR loop

# Compute n! (n is a positive integer)

READ n

fact ← 1

FOR i ← 1 TO n DO

fact ← fact * i

END FOR

WRITE "Factorial of " + n + " = " + fact

Example 5 – Unit conversion (Celsius ↔ Fahrenheit)

# Celsius to Fahrenheit conversion

CONST FA ← 9 / 5 # 9/5 = 1.8 (real constant)

READ C # temperature in Celsius (REAL)

F ← FA * C + 32

WRITE "Celsius = " + C

WRITE "Fahrenheit = " + F

Example 6 – Using a built‑in function (distance from origin)

# Pythagoras theorem

READ x

READ y

distance ← SQRT( POWER(x,2) + POWER(y,2) )

WRITE "Distance from origin = " + distance

Practice Questions

  1. Multiple of 5 or 7

    Write pseudocode that reads an integer n and outputs TRUE if n is a multiple of 5 or 7, otherwise FALSE.


    Mark‑scheme hint: use the modulo operator and an OR logical test.

  2. Modular arithmetic with squares

    Write pseudocode that reads two numbers x and y, computes (x² + y²) mod 10, and displays the result.


    Mark‑scheme hint: use POWER(x,2) and mod.

  3. Temperature conversion

    Write pseudocode that reads a temperature in Celsius, converts it to Fahrenheit using F = (9/5)·C + 32, and prints both values.


    Mark‑scheme hint: declare a constant for 9/5 (or write the fraction directly).

  4. Factorial (loop)

    Write pseudocode that reads a positive integer n, calculates n! using a loop, and writes the result.


    Mark‑scheme hint: a FOR loop from 1 to n is the simplest approach.

  5. Pass/Fail using IF‑THEN‑ELSE

    Write pseudocode that reads three exam marks, computes the average, and writes “PASS” if the average is at least 50, otherwise writes “FAIL”.


    Mark‑scheme hint: use the relational operator inside an IF statement.

Suggested flow‑chart (generic sequence): READ → CALCULATE → WRITE. Add a decision diamond when a logical test (IF) is required, and a loop symbol when repetition is needed.