Computer ScienceExam code: 0984

Pseudocode and Flowcharts

Algorithm Design and Problem Solving

CIE pseudocode is its own style. It is not real Python or any other language. The rules are deliberately simple, so any student can read or write it regardless of which language they have learned. When a question asks for pseudocode, use the CIE keywords (OUTPUT, not print(...)): the 15-mark scenario schemes ignore minor syntax errors, but they credit the logic, so write it in the CIE style the examiner expects.

Variables and assignment

Variables hold values. In CIE pseudocode the arrow operator ← is the only assignment operator. A single = is not assignment in CIE pseudocode; it means equality (comparison), so do not use it to give a variable a value.

Total ← 0
Count ← Count + 1
Name ← "Alice"
IsFinished ← FALSE

Variables are also declared with a data type using DECLARE:

DECLARE Score : INTEGER
DECLARE Name : STRING
DECLARE IsFinished : BOOLEAN
DECLARE Price : REAL

Input and output

Two clean keywords, with no brackets:

INPUT Age
OUTPUT "Welcome to the site, ", Name

IF-THEN-ELSE selection

A standard two-branch selection:

IF Age >= 18
  THEN
    OUTPUT "Welcome to the site"
  ELSE
    OUTPUT "Sorry, this site is for over-18s only"
ENDIF

Selections can be nested to test more than two cases:

IF Score >= 70
  THEN
    Grade ← "A"
  ELSE
    IF Score >= 60
      THEN
        Grade ← "B"
      ELSE
        Grade ← "C"
    ENDIF
ENDIF

For more than a handful of branches, CASE OF ... OTHERWISE ... ENDCASE is cleaner:

CASE OF Day
   1 : OUTPUT "Monday"
   2 : OUTPUT "Tuesday"
   3 : OUTPUT "Wednesday"
   4 : OUTPUT "Thursday"
   5 : OUTPUT "Friday"
   OTHERWISE OUTPUT "Weekend"
ENDCASE

Loops

Three loop constructs are tested by the syllabus; they are compared in section 5.

FOR i ← 1 TO 10
   OUTPUT i
NEXT i

WHILE NotFinished = TRUE DO
   INPUT Answer
   IF Answer = "stop"
     THEN
       NotFinished ← FALSE
   ENDIF
ENDWHILE

REPEAT
   INPUT Number
UNTIL Number > 0

Arrays

Arrays hold a list of values of the same type:

DECLARE Scores : ARRAY[1:10] OF INTEGER
Scores[1] ← 75
Scores[2] ← 88
OUTPUT Scores[1]

CIE arrays are usually 1-indexed, with the size given as [1:10]. Some other syntaxes use [10].

Arithmetic operators

OperatorMeaning
+ - * /Plus, minus, multiply, divide
DIV(a, b)Integer division: the quotient of a divided by b with the fractional part discarded, so DIV(17, 5) returns 3
MOD(a, b)The remainder of a divided by b, so MOD(17, 5) returns 2

Comparison operators

OperatorMeaning
=Equal to
<>Not equal to
< >Less than, greater than
<= >=Less than or equal, greater than or equal

Logical operators

OperatorMeaning
ANDBoth conditions are true
ORAt least one condition is true
NOTThe single condition is not true

Useful string functions

FunctionWhat it does
LENGTH(s)Returns the number of characters in s
SUBSTRING(s, start, length)Returns part of s starting at position start, for length characters
UCASE(s)Returns s with every letter in uppercase
LCASE(s)Returns s with every letter in lowercase

Procedures and functions

PROCEDURE Greet(Name : STRING)
   OUTPUT "Hello, ", Name
ENDPROCEDURE

CALL Greet("Alice")
FUNCTION Square(Value : INTEGER) RETURNS INTEGER
   RETURN Value * Value
ENDFUNCTION

Result ← Square(5)

These are covered in detail in topic 24 (Procedures and Functions).

Common pseudocode pitfalls

  • Do not use real-language syntax: avoid print(), input(), range(). Stick to OUTPUT, INPUT, FOR ... NEXT.
  • Always close compound statements: IF needs ENDIF; WHILE needs ENDWHILE; FOR needs NEXT <counter>.
  • Indent inside compound statements to make the structure clear; examiners reward readable layout.
  • Use UPPERCASE for keywords to make them stand out (IF, THEN, WHILE, DO, NEXT).
  • Use clear variable names that describe what they store (StudentScore, not x).

Common exam question

Finding and correcting the errors in a pseudocode algorithm

Question: A numbered pseudocode algorithm and its purpose are given; identify the errors, or their line numbers, and suggest a correction for each (3–5 marks, one per error).

Asked in 6 of the 17 papers. Each mark is for an error and its correction together, so give both.

Check each line against the stated purpose, since the planted errors repeat: a DECLARE with the wrong data type (a counter as STRING); the wrong keyword (IF opening a loop, ENDCASE closing an IF, WHILE for UNTIL); a wrong condition or loop limit (= for <>, OR for AND, > for <, a FOR loop starting at 0 or ending at 1); the wrong variable or array index (a total that adds the counter); and the wrong operator or start value (a total multiplied, a counter starting at 100). One further paper set it on a flowchart: swapped Yes/No labels, OR for AND, a reversed assignment, a wrong symbol.

Worked example

Correcting a faulty counting algorithm

This algorithm is intended to input 20 temperatures, count how many are below zero and output that count. It contains four errors. Identify each error by its line number and suggest a correction.

01 DECLARE Count : STRING
02 Count ← 0
03 FOR Index ← 1 TO 20
04     INPUT Temp
05     IF Temp > 0
06       THEN
07         Count ← Count + 1
08     ENDCASE
09 NEXT Index
10 OUTPUT "Below zero: ", Temp

Solution:

Test each line against the purpose: a count is a whole number, "below zero" is a less-than test, an IF closes with ENDIF, and the output must be the count.

  • Line 01: DECLARE Count : STRING should be DECLARE Count : INTEGER, because a count is a whole number
  • Line 05: IF Temp > 0 should be IF Temp < 0, because below zero means less than zero
  • Line 08: ENDCASE closes a CASE statement, not an IF, so it should be ENDIF
  • Line 10: OUTPUT "Below zero: ", Temp should be OUTPUT "Below zero: ", Count, because the count is wanted, not the last temperature

Each line and its correction together earn one mark. The other six lines are correct, so leave them alone: the FOR has its matching NEXT, the INPUT reads into Temp, and the THEN is where it belongs.

Build on this topic