Writing Pseudocode in the Exam (Cambridge (CIE) A Level Computer Science): Revision Note
Exam code: 9618
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 |
|---|---|
|
|
|
|
|
|
| no |
|
|
|
|
|
|
| no equivalent: loop through the index with a |
|
|
|
|
|
|
|
|
For changing case, use
TO_UPPERandTO_LOWERfrom the Insert; the Pseudocode Guide listsUCASEandLCASEinstead, 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,PROCEDUREIdentifiers (names of variables, constants and modules) are in mixed case, such as
NumberOfPlayersThey may only contain letters, digits and underscores, and must start with a letter
They must never be keywords, so
String,InputandIntegerare not valid namesThey are not case sensitive, so
TotalandTOTALare 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,BOOLEANorDATEConstants use
=and can only be given a literal valueArrays 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 withTYPE ... ENDTYPE, and an enumerated type withTYPE Season = (Spring, Summer, ...); examiners report students usingENUMorDECLAREwhereTYPEwas 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) andMOD(remainder)/always gives aREALresult, even if both values are integers
Comparison:
=,<>,<,>,<=,>=Logic:
AND,OR,NOT; each side ofANDandORmust be a complete conditionConcatenation:
&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
IFneedsTHENand a matchingENDIFCASEclauses are checked in order, and only the first one that matches runs;OTHERWISEmust come lastA
CASEclause can test a range, such as1 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
FORloops include the←and repeat the loop variable afterNEXT; addSTEPto count in steps other than 1 (including negative steps)REPEAT ... UNTILalways runs at least onceWHILE ... ENDWHILEmay 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 ownA function is never called with
CALL; it is used inside an expression, such as an assignment, anIFcondition or anOUTPUTWhen you call a module, write only its name and the values, not the full header with data types
Parameters are passed by value unless
BYREFis written before them; once written,BYREFalso 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,WRITEorAPPENDmode before using it;WRITEcreates a new file and wipes any existing dataUse
READFILEandWRITEFILE(notREADorWRITEon their own) and close every file withCLOSEFILEThe file name must be in quotes when it is written directly, and must be included in
EOF()andCLOSEFILE
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 |
|---|---|---|
| The assignment arrow is missing |
|
|
|
|
| Each side of |
|
| The second condition has no left-hand side |
|
|
|
|
| A keyword is used as an identifier |
|
| Functions are not called with |
|
| The index belongs to the array of records, in square brackets |
|
| A file name written directly needs quotes |
|
|
|
|
| This is programming language syntax | Loop through the indexes with |
To practise finding and correcting errors like these in a given algorithm, see Test Data & Finding Errors
Missing end statements
Examiners report that many marks are lost through small omissions, such as:
a correct procedure or function header with no
ENDPROCEDUREorENDFUNCTIONan
IFwith noENDIFa loop with no
ENDWHILE,UNTILorNEXT
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, andENDFUNCTION[1 mark]Declare and initialise
Totalusing←[1 mark]FORloop from 1 to 30 withNEXT Index[1 mark]Add each element to
Totalusing←inside the loop [1 mark]RETURNthe total divided by 30 [1 mark]
range(30)counts from 0 to 29, so with a lower bound of 1 it would try to readMarks[0], which does not exist, and would missMarks[30]printoutputs 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 statementIn a function, finish with
RETURN, notOUTPUT: examiners regularly seeOUTPUTused where the value should be returned, and it does not gain the mark
Unlock more, it's free!
Was this revision note helpful?