Writing Pseudocode in the Exam (Cambridge (CIE) A Level Computer Science): Revision Note

Exam code: 9618

Robert Hampton

Written by: Robert Hampton

Reviewed by: Bridgette Barrett

Updated on

Why Cambridge pseudocode matters

What is Cambridge pseudocode?

  • Cambridge pseudocode is the standard way of writing algorithms in 9618 exams

  • Its rules are set out in the Pseudocode Guide for Teachers, published by Cambridge for each syllabus period

  • Questions in Paper 2 and Paper 3 ask for answers in pseudocode, and any pseudocode printed in those papers is written in this style (Paper 3 also prints assembly language and declarative code, which follow their own rules)

  • Examiners state that following the recommended pseudocode lets them follow the structure and logic of an answer and credit it; answers written in a programming language are not accepted when a question asks for pseudocode

  • For how to work through Paper 2 itself, including using the Insert, see the Paper 2 guide

Why do Python users lose marks?

  • Most students practise programming in Python (or Java or Visual Basic) for Paper 4

  • Examiners warn that the syntax of the language you have studied is not acceptable in a pseudocode answer

  • The table shows the most common Python habits and the Cambridge equivalent

Python habit

Cambridge pseudocode

x = 5

X ← 5

if x == 5:

IF X = 5 THEN

!=

<>

elif

no ELSEIF: use a nested IF inside ELSE, or a CASE statement

"Hi " + name

"Hi " & Name

// and %

DIV and MOD

for i in range(1, 11):

FOR Index ← 1 TO 10 ... NEXT Index

for item in items:

no equivalent: loop through the index with a FOR loop

len(s), s.upper()

LENGTH(S), TO_UPPER(S) (from the Insert)

print() and input()

OUTPUT and INPUT

return inside def

RETURN inside FUNCTION ... RETURNS <data type>

# comment

// comment

  • For changing case, use TO_UPPER and TO_LOWER from the Insert; the Pseudocode Guide lists UCASE and LCASE instead, but the Insert is what you have in the exam, so use its function names

Conventions examiners check

How do you set out pseudocode?

  • Keywords are in upper case: IF, WHILE, PROCEDURE

  • Identifiers (names of variables, constants and modules) are in mixed case, such as NumberOfPlayers

    • They may only contain letters, digits and underscores, and must start with a letter

    • They must never be keywords, so String, Input and Integer are not valid names

    • They are not case sensitive, so Total and TOTAL are the same identifier

  • Indent the contents of every IF, loop and module (usually by three spaces)

  • Comments start with //

Declarations and assignment

  • Declare each variable with its data type: INTEGER, REAL, CHAR, STRING, BOOLEAN or DATE

  • Constants use = and can only be given a literal value

  • Arrays use square brackets and state the lower and upper bound

  • Assignment always uses the arrow ←; the = sign is only for comparison (and constants)

  • On Paper 3, define user-defined types with TYPE: a record with TYPE ... ENDTYPE, and an enumerated type with TYPE Season = (Spring, Summer, ...); examiners report students using ENUM or DECLARE where TYPE was needed

DECLARE Count : INTEGER
DECLARE Average : REAL
CONSTANT MaxStudents = 30
DECLARE Names : ARRAY[1:30] OF STRING
DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR
Count ← 0
Names[1] ← "Ali"
  • String values go in double quotes ("Ali") and single characters in single quotes ('A')

  • To access a field of a record stored in an array, put the index on the array, then use dot notation: Batch[Index].Weight

Operators

  • Arithmetic: +, -, *, /, DIV (whole-number quotient) and MOD (remainder)

    • / always gives a REAL result, even if both values are integers

  • Comparison: =, <>, <, >, <=, >=

  • Logic: AND, OR, NOT; each side of AND and OR must be a complete condition

  • Concatenation: &

  • Use brackets to make the order of operations clear

Selection

IF Score >= 50 THEN
   OUTPUT "Pass"
ELSE
   IF Score >= 40 THEN
      OUTPUT "Resit"
   ELSE
      OUTPUT "Fail"
   ENDIF
ENDIF

CASE OF Grade
   'A' : Points ← 10
   'B' : Points ← 8
   OTHERWISE : Points ← 0
ENDCASE
  • Every IF needs THEN and a matching ENDIF

  • CASE clauses are checked in order, and only the first one that matches runs; OTHERWISE must come last

  • A CASE clause can test a range, such as 1 TO 10 : ...

Iteration

FOR Index ← 1 TO 10
   Total ← Total + Marks[Index]
NEXT Index

REPEAT
   INPUT Password
UNTIL Password = "Secret"

WHILE Number > 9
   Number ← Number - 9
ENDWHILE
  • FOR loops include the ← and repeat the loop variable after NEXT; add STEP to count in steps other than 1 (including negative steps)

  • REPEAT ... UNTIL always runs at least once

  • WHILE ... ENDWHILE may run zero times, because the condition is tested first

Procedures and functions

PROCEDURE ShowTotal(Total : INTEGER)
   OUTPUT "The total is ", Total
ENDPROCEDURE

FUNCTION Max(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
   IF Number1 > Number2 THEN
      RETURN Number1
   ELSE
      RETURN Number2
   ENDIF
ENDFUNCTION

CALL ShowTotal(25)
Biggest ← Max(Score1, Score2)
  • A procedure is called with CALL, as a statement on its own

  • A function is never called with CALL; it is used inside an expression, such as an assignment, an IF condition or an OUTPUT

  • When you call a module, write only its name and the values, not the full header with data types

  • Parameters are passed by value unless BYREF is written before them; once written, BYREF also applies to the parameters that follow it in the same header

File handling

OPENFILE "Stock.txt" FOR READ
WHILE NOT EOF("Stock.txt")
   READFILE "Stock.txt", LineOfText
   OUTPUT LineOfText
ENDWHILE
CLOSEFILE "Stock.txt"
  • Open a file in READ, WRITE or APPEND mode before using it; WRITE creates a new file and wipes any existing data

  • Use READFILE and WRITEFILE (not READ or WRITE on their own) and close every file with CLOSEFILE

  • The file name must be in quotes when it is written directly, and must be included in EOF() and CLOSEFILE

Mistakes that cost marks

Which pseudocode errors do examiners see most?

  • Cambridge examiner reports list invalid pseudocode like the examples below; each one loses marks

What students wrote

What is wrong

Correct pseudocode

FOR X 1 TO 100

The assignment arrow is missing

FOR X ← 1 TO 100

FOR Index ← 1 TO EOF(Filename)

EOF returns TRUE or FALSE, not a number of lines

WHILE NOT EOF(Filename)

IF X = 3 OR 4 OR 5 THEN

Each side of OR must be a full comparison

IF X = 3 OR X = 4 OR X = 5 THEN

WHILE Number >= 100 AND <= 200

The second condition has no left-hand side

WHILE Number >= 100 AND Number <= 200

IF Num ← 43 THEN

← assigns a value; it does not compare

IF Num = 43 THEN

DECLARE String : STRING

A keyword is used as an identifier

DECLARE UserName : STRING

CALL Valid(MyString) then IF RESULT = TRUE THEN

Functions are not called with CALL, and RESULT does not exist

IF Valid(MyString) = TRUE THEN

Batch.Weight(Index)

The index belongs to the array of records, in square brackets

Batch[Index].Weight

OPENFILE Stock.txt FOR READ

A file name written directly needs quotes

OPENFILE "Stock.txt" FOR READ

OUTPUT "Total: " & Total (where Total is an INTEGER)

& only joins strings, so an integer must be converted or separated with a comma

OUTPUT "Total: ", Total or OUTPUT "Total: " & NUM_TO_STR(Total)

FOR WebName in Secret

This is programming language syntax

Loop through the indexes with FOR Index ← 1 TO ...

Missing end statements

  • Examiners report that many marks are lost through small omissions, such as:

    • a correct procedure or function header with no ENDPROCEDURE or ENDFUNCTION

    • an IF with no ENDIF

    • a loop with no ENDWHILE, UNTIL or NEXT

Worked Example

A global 1D array Marks stores the test marks of 30 students. The lower bound of the array is 1.

A student was asked to write pseudocode for a function Average() that returns the mean of the marks in Marks. This is their answer:

def Average():
   total = 0
   for i in range(30):
      total += Marks[i]
   print(total / 30)

Rewrite the answer as valid Cambridge pseudocode.

[5]

Answer:

FUNCTION Average() RETURNS REAL
   DECLARE Total : INTEGER
   DECLARE Index : INTEGER
   Total ← 0
   FOR Index ← 1 TO 30
      Total ← Total + Marks[Index]
   NEXT Index
   RETURN Total / 30
ENDFUNCTION
  • Function header with RETURNS REAL, and ENDFUNCTION [1 mark]

  • Declare and initialise Total using ← [1 mark]

  • FOR loop from 1 to 30 with NEXT Index [1 mark]

  • Add each element to Total using ← inside the loop [1 mark]

  • RETURN the total divided by 30 [1 mark]

  • range(30) counts from 0 to 29, so with a lower bound of 1 it would try to read Marks[0], which does not exist, and would miss Marks[30]

  • print outputs the value instead of returning it to the calling code

Examiner Tips and Tricks

  • Write the closing keyword (ENDIF, NEXT, ENDWHILE, ENDFUNCTION) straight after the opening line, then fill in the body, so you never lose a mark for a missing end statement

  • In a function, finish with RETURN, not OUTPUT: examiners regularly see OUTPUT used where the value should be returned, and it does not gain the mark

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Curriculum Expert

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

Bridgette Barrett

Reviewer: Bridgette Barrett

Expertise: Development Editor

After graduating with a degree in Geography, Bridgette completed a PGCE over 30 years ago. She later gained an MA Learning, Technology and Education from the University of Nottingham focussing on online learning. At a time when the study of geography has never been more important, Bridgette is passionate about creating content which supports students in achieving their potential in geography and builds their confidence.