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)

Type Typical Range / Description Exam‑language equivalents
INTEGER Whole numbers, usually –2 147 483 648 … 2 147 483 647 (32‑bit signed) Java int, Python int, VB Integer
REAL Numbers with a fractional part; single‑precision (≈ ±3.4 × 10³⁸) Java float, Python float, VB Single
BOOLEAN Logical values TRUE or FALSE Java boolean, Python bool, VB Boolean
CHARACTER Single symbol, e.g. ‘A’, ‘9’, ‘$’ Java char, Python str (length 1), VB Char
STRING Sequence of characters, e.g. “Hello World” Java String, Python str, VB String
CONST Constant value that cannot be changed after initialisation Java final, Python convention (uppercase name), VB Const
ARRAY Ordered collection of elements of the same type Java int[], Python list, VB Integer()
RECORD Structure 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 language Declaration example
Java int age = 0;
Python age = 0 # type inferred as int
Visual Basic Dim 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
    • snake_casetotal_score
  • Do not use reserved words as identifiers. Some examples: IF, FOR, WHILE, END, DECLARE, TRUE, FALSE.

Examples of Variable Declarations

Identifier TYPE Initial value Pseudocode Java Python VB
age INTEGER 0 DECLARE age : INTEGER ← 0 int age = 0; age = 0 Dim age As Integer = 0
price REAL 9.99 DECLARE price : REAL ← 9.99 float price = 9.99f; price = 9.99 Dim price As Single = 9.99
isValid BOOLEAN FALSE DECLARE isValid : BOOLEAN ← FALSE boolean isValid = false; isValid = False Dim isValid As Boolean = False
grade CHARACTER 'A' DECLARE grade : CHARACTER ← 'A' char grade = 'A'; grade = 'A' Dim grade As Char = "A"
name STRING "" (empty) DECLARE name : STRING ← "" String name = ""; name = "" Dim name As String = ""
MAX_STUDENTS CONST INTEGER 30 DECLARE MAX_STUDENTS : CONST INTEGER ← 30 final int MAX_STUDENTS = 30; MAX_STUDENTS = 30 # convention: upper‑case constant Const MAX_STUDENTS As Integer = 30
scores ARRAY[1..30] OF INTEGER DECLARE scores : ARRAY[1..30] OF INTEGER int[] scores = new int[30]; scores = [0]*30 Dim scores(30) As Integer
student RECORD DECLARE 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

✅ Item What to Verify
All identifiers declared before first use Search the algorithm for any variable that appears without a preceding DECLARE.
Correct Cambridge TYPE INTEGER, REAL, BOOLEAN, CHARACTER, STRING, CONST, ARRAY, RECORD – matches the intended data.
Initial value matches TYPE Numbers for INTEGER/REAL, TRUE/FALSE for BOOLEAN, quotes for CHARACTER/STRING.
No reserved words used as identifiers Check against the list: IF, THEN, ELSE, FOR, WHILE, END, DECLARE, TRUE, FALSE, etc.
Scope is appropriate Global only when needed by more than one sub‑algorithm; otherwise declare locally.
Constants declared with CONST All values that never change (e.g., MAX_STUDENTS) use the CONST keyword.
Declaration block placed at the very start First 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.

Create an account or Login to take a Quiz

107 views
0 improvement suggestions

Log in to suggest improvements to this note.