Cambridge A-Level Computer Science 9618 – 11.1 Programming Basics11.1 Programming Basics
Objective
Write pseudocode statements for the declaration of variables.
Why Declare \cdot ariables?
In pseudocode a variable must be declared before it is used so that the computer knows:
- The identifier (name) of the variable.
- What type of data it can store (e.g., integer, real, Boolean).
- Optionally, an initial value.
Common Data Types
- Integer – whole numbers, e.g., \$-3\$, \$0\$, \$42\$.
- Real – numbers with a fractional part, e.g., \$3.14\$, \$-0.001\$.
- Boolean – logical values
TRUE or FALSE. - Character – a single symbol, e.g.,
'A', '9'. - String – a sequence of characters, e.g.,
"Hello".
Pseudocode Syntax for Declaration
The generic form is:
DECLARE <identifier> : <type> [← <initial‑value>]
Square brackets indicate that the initialisation part is optional.
Examples of \cdot ariable Declarations
| Variable | Type | Initial \cdot alue (optional) | Pseudocode |
|---|
| age | Integer | 0 | DECLARE age : Integer ← 0 |
| price | Real | 9.99 | DECLARE price : Real ← 9.99 |
| is \cdot alid | Boolean | FALSE | DECLARE is \cdot alid : Boolean ← FALSE |
| grade | Character | 'A' | DECLARE grade : Character ← 'A' |
| name | String | "" | DECLARE name : String ← "" |
Step‑by‑Step Procedure for Declaring \cdot ariables
- Identify the purpose of each variable.
- Choose an appropriate data type.
- Write the
DECLARE statement using the syntax above. - If a sensible starting value exists, include it after the arrow (
←). - Place all declarations at the beginning of the algorithm or in a dedicated section.
Common Pitfalls
- Omitting the type – the compiler cannot infer the type in strict pseudocode.
- Using a name that conflicts with a reserved word (e.g.,
IF, WHILE). - Assigning an initial value that does not match the declared type.
Suggested diagram: Flow of an algorithm showing a “Variable Declaration” block before the main processing steps.