Write pseudocode statements for: the assignment of values to variables

11.1 Programming Basics – Assignment of Values to Variables

Context & Syllabus Alignment

  • Syllabus item: 11.1 Programming Basics (Cambridge International AS & A Level Computer Science 9618).
  • This lesson introduces the assignment statement, the required pseudocode syntax, the rules for variable names, data‑type compatibility, constants, input/output and built‑in functions.
  • Assessment objectives covered:
    • AO1 – Knowledge: definition of assignment, assignment operator, naming rules, type‑compatibility and constant rules.
    • AO2 – Application: write correct pseudocode statements that assign values to variables of different types, including I/O.
    • AO3 – Design & Programming: use assignment correctly inside larger algorithmic structures such as loops and procedures.
  • Suggested sequence: data types → literals → declaration & constants → assignment → input/output → control structures → procedures/functions → arrays/records → algorithm design & testing.

Key Terms (as used in the syllabus)

Assignment – the operation that stores the result of an expression in a variable, using the operator .
Variable name – an identifier that follows the board’s naming rules.
Type‑compatible – the right‑hand side expression must be of the same data type as the variable, or be one of the permitted implicit conversions.
Constant – a named value that cannot be altered after its initial definition; it may appear on the right‑hand side of := but never on the left.
Built‑in function – a predefined operation (e.g. LEN(), INT()) that can be used in an expression.

1. Declaration & Constants

Although many exam questions omit explicit declarations, the board expects you to understand the syntax.

PurposePseudocode
Declare an integer variableDECLARE counter: INTEGER
Declare and initialise a real variableDECLARE radius : REAL
RADIUS ← 3.5
Define a constant (cannot be on the left‑hand side of )CONSTANT PI ← 3.14159
Declare an array of five integersDECLARE marks: ARRAY[1:5] OF INTEGER
Declare a record type
TYPE student
    DECLARE name: STRING
    DECLARE mark: INTEGER
ENDTYPE

2. Assignment Syntax

The assignment operator in Cambridge pseudocode is . The general form is:

variable ← expression

After execution, variable holds the value produced by expression

2.1 Rules for Variable Names

  • Start with a letter (A–Z or a–z).
  • Subsequent characters may be letters, digits (0–9) or an underscore (_).
  • Case‑insensitive, but keep the same spelling throughout a program.
  • Do not use reserved words (e.g. IF, FOR, PROCEDURE.
  • Prefer meaningful names that reflect the data stored.

2.2 Data Types, Literals and Implicit Conversions

Data TypeLiteral ExampleDescription
Integer42Whole numbers, positive or negative.
Real3.14Numbers with a fractional part.
BooleanTRUE, FALSELogical values.
String"Hello"Sequence of characters enclosed in double quotes.

Only the following implicit conversions are allowed:

From (source)To (target)Result
IntegerRealInteger automatically becomes a real (e.g. 5 = 5.0).
CharacterStringSingle character becomes a one‑character string.
BooleanInteger / RealTRUE → 1, FALSE → 0.

All other conversions cause a type error.

2.3 Error Handling in Assignment

  • Type error: illegal conversion. Fix: use an explicit conversion function (e.g. INT()) or redesign the expression.
  • Uninitialised variable: variable used on the right‑hand side before a value has been given. Fix: initialise at declaration or before first use.
  • Out‑of‑range error: value exceeds the permitted range for the target type. Fix: apply rounding/truncation or use a larger type.
  • Constant‑assignment error: attempting to assign to a constant. Fix: treat the constant as read‑only.

3. Common Forms of Assignment (with Examples)

3.1 Simple Value Assignment

DECLARE  age: INTEGER
age ← 18

3.2 Arithmetic Assignment

INTEGER a, b, sum
a ← 7
b ← 5
sum ← a + b   // sum now holds 12

3.3 Compound Assignment (using the variable’s existing value)

DECLARE counter : INTEGER
counter ← 0
counter ← counter + 1   // increment
counter ← counter * 2   // double

3.4 Assignment to Array Elements

DECLARE scores: ARRAY[1:5] OF INTEGER
i ← 3
scores[i] ← 87   // stores 87 in the 4th element (indexing starts at 0)

3.5 Assignment to Record/ADT Fields

TYPE student
    DECLARE name: STRING
    DECLARE mark: INTEGER
ENDTYPE

DECLARE thisStudent ← : student
thisStudent.name ← "Bob"
thisStudent.mark ← 73

3.6 Using Expressions

DECLARE radius, area: REAL 
radius ← 3.5
area ← 3.14159 * radius * radius   // πr²

3.7 String Concatenation

DECLARE firstName, lastName, fullName : STRING
firstName ← "Ada"
lastName  ← "Lovelace"
fullName  ← firstName & " " & lastName   // "Patrick Smith"

3.8 Boolean Assignment

DECLARE score: INTEGER
DECLARE isPass: BOOLEAN 
score ← 78
isPass ← score >= 50   // TRUE

3.9 Using a Built‑in Function

DECLARE  name : STRING
DECLARE length : INTEGER 
INPUT name
length ← LEN(name)          // LEN returns the number of characters
OUTPUT "The name has ", length, " letters."

4. Assignment Inside Larger Structures (AO3)

Below is a compact algorithm that demonstrates declaration, input, assignment, a loop, array usage and output.

PROCEDURE ReadMarksAndTotal()
    DECLARE i, total : INTEGER 
    DECLARE marks:  ARRAY[1:5] OF INTEGER 
    total ← 0
    FOR i ← 0 TO 4 
        READ marks[i]                 // input each mark
        total ← total + marks[i]     // accumulate using assignment
    NEXT i
    OUTPUT "Total of the 5 marks = ", total
ENDPROCEDURE

5. Common Mistakes to Avoid

  • Using = instead of := for assignment.
  • Assigning a value of an incompatible type (e.g., a string to an integer).
  • Attempting to assign to a constant.
  • Using a variable on the right‑hand side before it has been given a value.
  • Omitting the required semicolon when the exam specification asks for it.
  • Assuming illegal implicit conversions are allowed (e.g., string → integer).

6. Practice Exercises

  1. Declare an integer variable called counter and initialise it to 0.
  2. Read a student's mark (integer) and store TRUE in a Boolean variable passed if the mark is 40 or above.
  3. Given REAL length, width, area, assign the product of length and width to area.
  4. Create a string variable greeting that concatenates the word “Hello”, a space, and a variable name (which already holds a student's name).
  5. Using a single assignment statement, set three integer variables x, y, z to the values 10, 20, and 30 respectively.
  6. Assign the value 0 to every element of an integer array marks[5] using a loop.
  7. Given a record student with fields name (STRING) and score (INTEGER), write pseudocode that updates student.score to the value stored in variable newScore.
  8. Write a short program that reads a string word, uses the built‑in function LEN() to find its length, and writes “Length = ” followed by the length.
  9. Extend the algorithm in section 4 to also calculate and write the average mark (as a REAL).
Suggested diagram: Flow of an assignment statement – variable on the left, assignment operator in the centre, expression on the right.

Create an account or Login to take a Quiz

150 views
0 improvement suggestions

Log in to suggest improvements to this note.