Paper 2 ends with a 15-mark programming scenario. Everything here, plus the runner below, helps you write it confidently.

1. Variables and constants

A variable is a named memory location whose value can change while the program runs. A constant is a named value that cannot change, which makes programs easier to update and read.

DECLARE PlayerName : STRING
DECLARE Score : INTEGER
CONSTANT MaxLives ← 3
PlayerName ← "Ela"
Score ← 0

2. Data types

Data typeStoresExample
INTEGERWhole numbersScore ← 42
REALNumbers with a decimal partPrice ← 3.99
CHARA single characterGrade ← ‘A’
STRINGText (zero or more characters)Name ← “Ela”
BOOLEANTRUE or FALSEGameOver ← FALSE

3. Input and output

Use OUTPUT to display values and prompts, and INPUT to read a value from the user into a variable.

OUTPUT "Enter your age"
INPUT Age
OUTPUT "In 5 years you will be ", Age + 5

4. Sequence and selection

Sequence means statements run one after another in order. Selection chooses which statements run, using IF or CASE. The example below also shows nested selection: an IF inside another IF.

IF Temperature > 30
  THEN
    OUTPUT "Hot"
  ELSE
    IF Temperature > 15
      THEN
        OUTPUT "Warm"
      ELSE
        OUTPUT "Cold"
    ENDIF
ENDIF

CASE OF Choice
  1 : OUTPUT "Play"
  2 : OUTPUT "Settings"
  3 : OUTPUT "Quit"
  OTHERWISE OUTPUT "Invalid choice"
ENDCASE

5. Iteration

LoopPseudocodeWhen to use it
Count-controlledFOR … TO … NEXTWhen you know how many times to repeat
Pre-conditionWHILE … DO … ENDWHILEWhen the condition is checked before each repeat; it may run zero times
Post-conditionREPEAT … UNTILWhen the code must run at least once, e.g. validating input

Totalling (Total ← Total + Value) and counting (Count ← Count + 1) are done inside loops; see Unit 7 for full examples.

6. String handling

Word ← "Computer"
OUTPUT LENGTH(Word)              // 8
OUTPUT SUBSTRING(Word, 1, 4)     // "Comp"
OUTPUT UCASE(Word)               // "COMPUTER"
OUTPUT LCASE(Word)               // "computer"

In Cambridge pseudocode the first character is at position 1. Some programming languages start at 0.

7. Operators

TypeOperators
Arithmetic+ − * / ^ (power), MOD (remainder), DIV (whole-number division)
Relational= < <= > >= <> (not equal to)
LogicalAND, OR, NOT

8. Procedures, functions and parameters

A procedure is a named block of code that performs a task; it is run with CALL. A function is similar but always returns a value, so it is used inside an expression. Parameters pass values into them (up to three in the exam).

PROCEDURE ShowLine(Size : INTEGER)
  FOR Count ← 1 TO Size
    OUTPUT "-"
  NEXT Count
ENDPROCEDURE

FUNCTION Area(Length : REAL, Width : REAL) RETURNS REAL
  RETURN Length * Width
ENDFUNCTION

CALL ShowLine(10)
OUTPUT Area(4.5, 2)

A local variable exists only inside the procedure or function where it is declared. A global variable can be used anywhere in the program.

9. Library routines

OUTPUT MOD(17, 5)                // 2: the remainder
OUTPUT DIV(17, 5)                // 3: whole-number division
OUTPUT ROUND(3.14159, 2)         // 3.14
Dice ← ROUND(RANDOM() * 5, 0) + 1  // a whole number from 1 to 6

10. Maintainable programs

  • Meaningful identifiers for variables, constants, arrays, procedures and functions, e.g. TotalScore, not T.
  • Comments (// in pseudocode) that explain what sections of code do.
  • Procedures and functions that break the program into reusable parts.

11. Arrays

An array stores many values of the same data type under one name. Each value is accessed by its index, which can be a variable. A 1D array is a list; a 2D array is a table of rows and columns. Use loops (and nested loops for 2D) to read and write values.

DECLARE Marks : ARRAY[1:5] OF INTEGER
FOR Index ← 1 TO 5
  INPUT Marks[Index]
NEXT Index

DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
FOR Row ← 1 TO 3
  FOR Column ← 1 TO 3
    Board[Row, Column] ← '-'
  NEXT Column
NEXT Row

12. File handling

Data stored in a file is kept after the program ends, so it can be used again later or by another program.

DECLARE LineOfText : STRING
OPENFILE "Scores.txt" FOR WRITE
WRITEFILE "Scores.txt", "Ela 85"
CLOSEFILE "Scores.txt"

OPENFILE "Scores.txt" FOR READ
READFILE "Scores.txt", LineOfText
OUTPUT LineOfText
CLOSEFILE "Scores.txt"

13. Run your own pseudocode

Try it: run your own pseudocode

Pick an example or write your own Cambridge-style pseudocode, add any inputs, and press Run. Supports DECLARE, CONSTANT, ←, INPUT, OUTPUT, IF, CASE, FOR, WHILE, REPEAT, arrays, procedures, functions and the library routines.

14. Examiner tips

  1. Write pseudocode, not program code, except in the 15-mark scenario question.
  2. Use meaningful names and comment your code in longer answers.
  3. Functions RETURN a value; procedures don’t. Call a procedure with CALL.
  4. Initialise totals and counters before the loop, and update them inside it.
  5. Close every structure (ENDIF, ENDCASE, NEXT, ENDWHILE, UNTIL, ENDPROCEDURE, ENDFUNCTION).
  6. MOD gives the remainder; DIV gives the whole-number part.
  7. Arrays: check the index range and use a loop to process every element.
  8. Files: OPENFILE before reading or writing, and CLOSEFILE afterwards.

15. Practice questions

Practice: answer and check instantly

Type a short answer, then press Check or Enter.

Show answer

BOOLEAN.

Show answer

CHAR.

Show answer

2: 17 = 3 × 5 + 2.

Show answer

3: whole-number division.

Show answer

8 characters.

Show answer

“put”: 3 characters starting at position 4.

Show answer

CALL.

Show answer

REPEAT … UNTIL.

Exam-style questions

9. Write a function called Average that takes three integer parameters and returns their average. [4]

Show mark guide

FUNCTION Average(A : INTEGER, B : INTEGER, C : INTEGER) RETURNS REAL [1 header with parameters, 1 RETURNS type] · RETURN (A + B + C) / 3 [1] · ENDFUNCTION [1].

10. Explain the difference between a local and a global variable. [2]

Show mark guide

A local variable is declared inside a procedure or function and can only be used there [1]; a global variable is declared in the main program and can be used anywhere in the program [1].

16. Topic test: predict your grade

Topic test: predict your grade

15 questions, about 10 minutes. Answer them all, then press Mark my test.

1. Which is a constant?
2. Which data type should store 3.75?
3. Which statement chooses between many values of one variable?
4. Which loop checks its condition before the first repeat?
5. What does UCASE(“hi”) return?
6. What is 2 ^ 3?
!=
8. What is the difference between a function and a procedure?
9. A variable that can only be used inside one procedure is…
10. Which library routine gives a random number?
11. Which makes a program easier to maintain?
12. How is the 3rd element of the array Marks written?
13. How many elements does ARRAY[1:3, 1:4] hold?
14. Why store data in a file?
15. Which must come before READFILE?

Frequently asked questions

What are the three basic programming constructs?

The three constructs are sequence, selection (IF and CASE) and iteration (FOR, WHILE and REPEAT loops).

What is the difference between a procedure and a function?

A procedure performs a task. A function performs a task and returns a single value that can be used in an expression.

What is the difference between a local and a global variable?

A local variable is declared inside a subroutine and can only be used there. A global variable is declared in the main program and can be used anywhere.

Was this page helpful?