Write pseudocode statements for: the declaration and initialisation of constants

11.1 Programming Basics – Declaration and Initialisation of Constants

Learning Objective

  • Write correct Cambridge‑style pseudocode statements to declare and initialise constants, and use them safely in any program construct.

Syllabus Alignment (Cambridge AS & A Level Computer Science 9618)

Syllabus Item What the note covers AO Requirement
11.1 Programming Basics – Constants Definition, syntax, scope, naming, type‑checking, examples, and use in data structures. AO1 – knowledge of constant declaration and initialisation.
11.2 Programming Constructs (IF, CASE, loops, procedures) Shows constants used inside IF, CASE, FOR/WHILE and procedures. AO3 – design and implementation of algorithms using constants.
Testing & Evaluation (AO3) Mentions constants in test data, expected results and documentation. AO3 – planning and evaluating programs.
Ethics & Ownership (AO3) Explains how constants reduce “magic numbers” and support maintainable, auditable code. AO3 – ethical considerations in software development.

Why Use Constants?

  • Readability: A meaningful name (e.g., MAX_STUDENTS) makes the purpose of a value obvious.
  • Maintainability: Changing the value in one place updates it everywhere it is used.
  • Safety: Once set, a constant cannot be altered, preventing accidental modification of critical data.
  • Ethical coding: Replacing “magic numbers” with named constants makes programs easier to audit and modify, supporting good professional practice.

Key Concepts

ConceptCambridge Definition
ConstantA named value that, once initialised, cannot be changed during program execution.
IdentifierThe name of a constant. Must start with a letter, may contain letters, digits and underscores, and is case‑sensitive.
InitialisationAssigning a value to a constant at the moment of declaration.
ScopeWhere the constant is visible. In Cambridge pseudocode a constant declared at the top of a program has **global scope**; a constant declared inside a procedure has **local scope** to that procedure.
Type‑checkingIf a type is supplied, the initial value must be compatible (e.g., an INTEGER constant cannot be given a string value).

Scope & Naming Rules (concise checklist)

  • Constants declared before any procedure have **global scope** – they can be used anywhere in the program.
  • Constants declared inside a procedure have **local scope** – they are visible only within that procedure.
  • Each identifier must be **unique** within its scope.
  • Allowed characters: letters (A‑Z, a‑z), digits (0‑9), underscore (_). Must start with a letter.
  • Cambridge convention: write constant names in **UPPERCASE** with underscores to separate words.

Type‑Checking Reminder

If a type is specified, the value must match that type. If no type is given, the constant is typeless but must still be used consistently throughout the algorithm.

Pseudocode Syntax (Cambridge)

Use the CONST keyword, followed by the identifier, an optional type, the assignment arrow , and the value.

CONST identifier [: type] ← value

Declaration & Initialisation – Example Table

Constant Name Type (optional) Value Pseudocode Statement
MAX_STUDENTS INTEGER 30 CONST MAX_STUDENTS : INTEGER ← 30
PI REAL 3.14159 CONST PI : REAL ← 3.14159
WELCOME_MSG STRING "Welcome to the system" CONST WELCOME_MSG : STRING ← "Welcome to the system"
GRAVITY REAL 9.81 CONST GRAVITY : REAL ← 9.81
DAYS_IN_WEEK INTEGER 7 CONST DAYS_IN_WEEK : INTEGER ← 7
TAX_RATE REAL 0.20 CONST TAX_RATE : REAL ← 0.20

Constants in Data Structures

Constants are often used to define array bounds, record field sizes, or fixed limits.

CONST MAX_SIZE : INTEGER ← 100
CONST NAME_LENGTH : INTEGER ← 30

DECLARE scores[MAX_SIZE] : INTEGER
DECLARE studentRecord :
    name : STRING[NAME_LENGTH]
    id   : INTEGER
    mark : INTEGER
END RECORD

Using Constants in Any Program Construct

Once declared, a constant can appear in IF, CASE, loops, and procedures – exactly as a literal value would, but with the benefits listed above.

CONST PI : REAL ← 3.14159
CONST MAX_ITER : INTEGER ← 10

PROCEDURE areaOfCircle(radius : REAL) RETURNS REAL
    RETURN PI * radius * radius
END PROCEDURE

PROCEDURE countDown()
    FOR i ← MAX_ITER DOWNTO 1 DO
        IF i MOD 2 = 0 THEN
            DISPLAY "Even step: " & i
        ELSE
            DISPLAY "Odd step: " & i
        END IF
    END FOR
END PROCEDURE

PROCEDURE gradeMark(mark : INTEGER) RETURNS STRING
    CASE mark OF
        0..39:   RETURN "F"
        40..49:  RETURN "D"
        50..59:  RETURN "C"
        60..69:  RETURN "B"
        70..100: RETURN "A"
    END CASE
END PROCEDURE

Constants in Testing & Documentation

  • When writing test data, use the same constants that appear in the program (e.g., expected result = PI * 2²).
  • Document each constant in the design specification – this satisfies AO3 requirements for clear, testable algorithms.

Ethics & Good Coding Practice

Replacing hard‑coded “magic numbers” with named constants makes the code:

  • Easier to understand for other developers.
  • Safer to modify, reducing the risk of introducing bugs.
  • More transparent for auditing, an essential professional responsibility.

Common Mistakes & How to Avoid Them

  • Omitting CONST – creates a variable that can be changed.
  • Using = instead of – not Cambridge‑compliant.
  • Type/value mismatch (e.g., assigning a string to an INTEGER constant).
  • Duplicate identifiers – each constant must be unique within its scope.
  • Declaring a constant inside a procedure when it is needed globally – leads to scope errors.

Practice Questions

  1. Declare a constant called MAX_SPEED of type REAL with a value of 120.5.
  2. Write pseudocode that uses the constant GRAVITY to compute the weight of an object given its mass (weight = mass × GRAVITY).
  3. Identify and correct the error in the following statement:
    CONST taxRate ← 0.15
  4. Define a constant MAX_SIZE and use it to declare an integer array named scores. Then write a loop that initialises every element of scores to zero.

Answers to Practice

1. CONST MAX_SPEED : REAL ← 120.5

2. PROCEDURE calculateWeight(mass : REAL) RETURNS REAL
       RETURN mass * GRAVITY
   END PROCEDURE

3. Error: identifier not in uppercase (convention) and type omitted (optional but clearer).  
   Corrected: CONST TAX_RATE : REAL ← 0.15

4. CONST MAX_SIZE : INTEGER ← 100
   DECLARE scores[MAX_SIZE] : INTEGER
   FOR i ← 1 TO MAX_SIZE DO
       scores[i] ← 0
   END FOR

Suggested Diagram (optional)

Flowchart showing constants declared at the start of a program and referenced later in calculations, loops and conditionals
Flowchart: constants are declared before the main algorithm and then used in subsequent processing steps (IF, LOOP, PROCEDURE).

Summary

  • Use CONST to declare a constant; optionally specify a type.
  • Initialise with the Cambridge assignment arrow .
  • Place global constants at the top of the program; local constants can be declared inside procedures.
  • Follow naming rules (UPPERCASE, letters/digits/underscores, unique).
  • Constants improve readability, safety, maintainability and support ethical coding practices.
  • They may be used in any construct (IF, CASE, loops, procedures) and are essential for reliable testing and documentation.

Create an account or Login to take a Quiz

89 views
0 improvement suggestions

Log in to suggest improvements to this note.