Write pseudocode statements for: the declaration of variables

11.1 Programming Basics – Declaring Variables

Learning Objective

  • Write correct pseudocode DECLARE statements, choose the appropriate Cambridge data type, initialise where required, and apply the correct scope rules.

Why Declare Variables?

In the Cambridge A‑Level Computer Science (9618) exam every algorithm must begin with a clear declaration of every identifier that will be used. Declaring a variable tells the computer:

  • Identifier – the name used later in the algorithm.
  • Data type – the kind of value it can store (INTEGER, REAL, …).
  • Initial value (optional) – the value it holds before any processing.
  • Scope – where in the algorithm the identifier is visible.

Cambridge Data Types (Exact Syllabus Names)

TypeTypical Range / DescriptionExam‑language equivalents
INTEGERWhole numbers, usually –2 147 483 648 … 2 147 483 647 (32‑bit signed)Java int, Python int, VB Integer
REALNumbers with a fractional part; single‑precision (≈ ±3.4 × 10³⁸)Java float, Python float, VB Single
BOOLEANLogical values TRUE or FALSEJava boolean, Python bool, VB Boolean
CHARACTERSingle symbol, e.g. ‘A’, ‘9’, ‘$’Java char, Python str (length 1), VB Char
STRINGSequence of characters, e.g. “Hello World”Java String, Python str, VB String
CONSTConstant value that cannot be changed after initialisationJava final, Python convention (uppercase name), VB Const
ARRAYOrdered collection of elements of the same typeJava int[], Python list, VB Integer()
RECORDStructure containing fields of different types (Cambridge term “record”)Java class, Python dict/custom class, VB Type … End Type

Pseudocode Declaration Syntax

The Cambridge specification uses a language‑independent form. The generic pattern is:

DECLARE <identifier> : <TYPE> [← <initial‑value>]

  • Square brackets [ ] denote an optional part.
  • The arrow is the initialisation operator – it may be used only in the declaration block. All later assignments also use , but the exam marks a loss of marks if a variable is used before it has been initialised.

Language‑Specific Variants (for exam practice)

Exam languageDeclaration example
Javaint age = 0;
Pythonage = 0 # type inferred as int
Visual BasicDim age As Integer = 0

Scope of Variables

  • Global (programme‑wide) scope: declared before the main algorithm block; visible to every sub‑algorithm.
  • Local scope: declared inside a sub‑algorithm (function, procedure, loop, or block); visible only within that block.
  • Variables needed in more than one sub‑algorithm should be declared globally; otherwise keep them local to avoid accidental modification.

Naming Conventions & Reserved Words

  • Identifiers may contain letters, digits and the underscore _. They must start with a letter.
  • Cambridge does not enforce a particular case style, but consistency is rewarded. Common choices:

    • camelCasetotalScore
    • snakecasetotalscore

  • Do not use reserved words as identifiers. Some examples: IF, FOR, WHILE, END, DECLARE, TRUE, FALSE.

Examples of Variable Declarations

IdentifierTYPEInitial valuePseudocodeJavaPythonVB
ageINTEGER0DECLARE age : INTEGER ← 0int age = 0;age = 0Dim age As Integer = 0
priceREAL9.99DECLARE price : REAL ← 9.99float price = 9.99f;price = 9.99Dim price As Single = 9.99
isValidBOOLEANFALSEDECLARE isValid : BOOLEAN ← FALSEboolean isValid = false;isValid = FalseDim isValid As Boolean = False
gradeCHARACTER'A'DECLARE grade : CHARACTER ← 'A'char grade = 'A';grade = 'A'Dim grade As Char = "A"
nameSTRING"" (empty)DECLARE name : STRING ← ""String name = "";name = ""Dim name As String = ""
MAX_STUDENTSCONST INTEGER30DECLARE MAX_STUDENTS : CONST INTEGER ← 30final int MAX_STUDENTS = 30;MAX_STUDENTS = 30 # convention: upper‑case constantConst MAX_STUDENTS As Integer = 30
scoresARRAY[1..30] OF INTEGERDECLARE scores : ARRAY[1..30] OF INTEGERint[] scores = new int[30];scores = [0]*30Dim scores(30) As Integer
studentRECORDDECLARE student : RECORD

name : STRING

age : INTEGER

grade: CHARACTER

END RECORD

class Student {

String name;

int age;

char grade;

}

student = {'name':'', 'age':0, 'grade':''}Type Student

name As String

age As Integer

grade As Char

End Type

Step‑by‑Step Procedure for Declaring Variables

  1. Identify the purpose of each identifier in the algorithm.
  2. Select the most appropriate TYPE (INTEGER, REAL, …). Consider range, precision, and whether the value is constant.
  3. Decide on scope – global if needed by several sub‑algorithms, otherwise local.
  4. Write the DECLARE statement using the exact Cambridge syntax.
  5. If a sensible starting value exists, initialise it with .
  6. Place all declarations at the very start of the algorithm or in a clearly marked “Variable Declaration” block.

Common Pitfalls (and How to Avoid Them)

  • Omitting the TYPE – every DECLARE must specify a Cambridge type (INTEGER, REAL, …).
  • Using a reserved word as an identifier – e.g. DECLARE IF : BOOLEAN is illegal.
  • Type‑mismatch in initialisation – do not assign a string to an INTEGER: DECLARE count : INTEGER ← "0".
  • Scope errors – a variable declared inside a loop cannot be used after the loop ends.

    FOR i ← 1 TO 10

    DECLARE total : INTEGER ← 0 ← ❌ local to the loop

    END FOR

    ← total ← total + i ← error – total out of scope

  • Incorrect Boolean initial value – only TRUE or FALSE are allowed.
  • For REAL variables remember the exam assumes single‑precision unless otherwise stated.

Exam‑Ready Checklist

✅ ItemWhat to Verify
All identifiers declared before first useSearch the algorithm for any variable that appears without a preceding DECLARE.
Correct Cambridge TYPEINTEGER, REAL, BOOLEAN, CHARACTER, STRING, CONST, ARRAY, RECORD – matches the intended data.
Initial value matches TYPENumbers for INTEGER/REAL, TRUE/FALSE for BOOLEAN, quotes for CHARACTER/STRING.
No reserved words used as identifiersCheck against the list: IF, THEN, ELSE, FOR, WHILE, END, DECLARE, TRUE, FALSE, etc.
Scope is appropriateGlobal only when needed by more than one sub‑algorithm; otherwise declare locally.
Constants declared with CONSTAll values that never change (e.g., MAX_STUDENTS) use the CONST keyword.
Declaration block placed at the very startFirst lines of the algorithm should be the DECLARE statements, optionally under a heading “Variable Declaration”.

Full‑Question Style Example

Exam Prompt (Cambridge style):

Write a pseudocode algorithm called CALCULATE_AVERAGE that:

  1. Reads the number of students n (1 ≤ n ≤ 30).
  2. For each student, reads the integer mark (0 – 100) and stores it in an array marks.
  3. Calculates the average (a REAL value) of the marks.
  4. Outputs the average to two decimal places.

Use appropriate declarations, initialise variables, and observe correct scope.

Solution (highlighting declarations):

ALGORITHM CALCULATE_AVERAGE

/*--- Variable Declaration Block ---*/

DECLARE n : INTEGER

DECLARE i : INTEGER ← 0

DECLARE sum : INTEGER ← 0

DECLARE average : REAL ← 0.0

DECLARE marks : ARRAY[1..30] OF INTEGER

/*--- Input ---*/

OUTPUT "Enter number of students (1‑30): "

INPUT n

/* n is guaranteed to be within range by the question */

/*--- Process ---*/

FOR i ← 1 TO n

OUTPUT "Enter mark for student ", i, ": "

INPUT marks[i]

sum ← sum + marks[i]

END FOR

average ← sum / n /* division yields REAL */

OUTPUT "Average mark = ", average:2 /* 2 decimal places */

END ALGORITHM

Alignment with Assessment Objectives

  • AO1 – Knowledge: Understand Cambridge data types, declaration syntax, constants, arrays, records, and scope.
  • AO2 – Application: Write correct DECLARE statements in pseudocode and translate them into Java, Python or VB where required.
  • AO3 – Design & Evaluation: Choose the most efficient type and scope, justify the use of constants, and evaluate the impact of initial values on algorithm correctness.