SDD  ·  Software Design & Development

Variables, Data Types & Arithmetic

Lesson SDD5 of 18 Approx 60 min
Learning intentions
  • Describe what a variable is and explain how it stores data in memory
  • Identify and describe the five data types: character, string, integer, real, boolean
  • Choose the correct data type for a given piece of data
  • Use assignment to store values in variables using SQA reference language
  • Use arithmetic operators (+, −, *, /, ^) in expressions
  • Explain operator precedence (BODMAS) and apply brackets correctly
  • Use string concatenation with the + operator
Success criteria
  • I can define the term variable
  • I can name all five N5 data types and give an example of each
  • I can select the correct data type for a described piece of data
  • I can write a DECLARE statement and an assignment (SET) in SQA reference language
  • I can use all five arithmetic operators in expressions
  • I can explain what BODMAS is and correct a BODMAS error in code
  • I can join strings together using concatenation
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.

1. Which of the following best describes pseudocode?

2. In a pseudocode design, what is the purpose of a refinement (e.g. step 2.1, 2.2)?

3. A program needs to repeat a set of steps exactly 5 times. Which pseudocode construct should be used?

Key vocabulary

variable
A named storage location in memory that holds a single item of data
data type
The kind of data a variable can hold, which determines what operations can be performed on it
character
A data type that stores a single letter, digit, or symbol
string
A data type that stores a sequence of characters (text)
integer
A data type that stores a positive or negative whole number with no decimal point
real
A data type that stores a positive or negative number that can include a decimal point
boolean
A data type that can only store the value TRUE or FALSE
declaration
The process of creating a variable and specifying its name, data type, and initial value
assignment
The process of storing a value in a variable using SET
expression
A combination of values, variables, and operators that the computer evaluates to produce a result
arithmetic operator
A symbol (+, −, *, /, ^) that performs a mathematical operation
concatenation
Joining two or more strings together using the + operator
BODMAS
The order in which arithmetic operations are carried out: Brackets, Orders, Division, Multiplication, Addition, Subtraction

Variables, Data Types & Arithmetic

What is a variable?

Every useful program needs to store data — values typed by the user, results of calculations, or information the program needs to remember. A variable is a named storage location in the computer's memory that holds a single item of data.

Think of a variable like a labelled box: the label is the variable's name, and whatever is inside the box is its current value. When you put something new in the box, the old contents are permanently replaced — a variable can only hold one value at a time.

Variables must be given a meaningful name that describes what they store. A variable called score is immediately understandable; a variable called x is not. Meaningful names make code easier to read, maintain, and debug.

Declaring a variable

Before a variable can be used in a program, it must be declared — this reserves a space in memory and tells the translator what kind of data it will hold. In SQA reference language the syntax is:

DECLARE variableName AS DATATYPE INITIALLY startingValue

Examples:

DECLARE score AS INTEGER INITIALLY 0
DECLARE playerName AS STRING INITIALLY ""
DECLARE average AS REAL INITIALLY 0.0
DECLARE firstLetter AS CHARACTER INITIALLY ""
DECLARE gameOver AS BOOLEAN INITIALLY FALSE

Note that Python does not require variable declarations — it works out the type automatically when the variable is first assigned a value. This is called dynamic typing. In the exam, always use SQA reference language syntax.

The five data types

A data type defines what kind of data a variable can store and what operations can be performed on it. There are five data types at N5:

1. Character
Stores a single letter, digit, or symbol. Internally the computer stores the ASCII code for that symbol.

DECLARE firstInitial AS CHARACTER INITIALLY ""
SET firstInitial TO "R"

Examples of character values: "A", "7", "@", "!"

2. String
Stores a sequence of characters — essentially any piece of text. A phone number is stored as a string (not an integer) because it may start with 0, and you never do arithmetic with it.

DECLARE username AS STRING INITIALLY ""
SET username TO "coolcoder99"

Examples: "Edinburgh", "L3tme!n#@", "0131 252 5000"

3. Integer
Stores a positive or negative whole number. No decimal point is possible.

DECLARE score AS INTEGER INITIALLY 0
SET score TO 42

Examples: 0, -6, 1000, -999. Key point: integers never appear in double quotes in code — quotes are for characters and strings only.

4. Real
Stores a positive or negative number that can include a decimal point. In other languages this may be called float or single, but in the N5 exam you must always use the term real.

DECLARE average AS REAL INITIALLY 0.0
SET average TO 7.35

Examples: 3.14, -0.5, 186.332

5. Boolean
Can only store two values: TRUE or FALSE. Booleans are commonly used as flags — to track whether something has happened yet.

DECLARE gameOver AS BOOLEAN INITIALLY FALSE
IF score > 100 THEN
    SET gameOver TO TRUE
END IF

Booleans connect directly to how all digital computers work at hardware level — everything ultimately comes down to 1s and 0s, true or false.

Choosing the right data type

Choosing the wrong data type is one of the most common mistakes at N5. Use this approach:

  • Is it a whole number you'll do arithmetic with? → Integer
  • Is it a number with a decimal point? → Real
  • Is it text, or a number you won't calculate with (like a postcode or phone number)? → String
  • Is it a single letter or symbol? → Character
  • Can it only ever be true or false? → Boolean
Text or a number you won't calculate? YES String NO Does it have a decimal point? YES Real NO Can it only ever be True or False? YES Boolean NO Integer

Assignment

Once a variable is declared, you store data in it using assignment:

SET variableName TO value

The computer reads the right-hand side first, works out the value, then stores the result in the variable on the left.

SET score TO 0
SET playerName TO "Maya"
SET average TO score / 10

Arithmetic operators

When working with integers and real numbers, you can use arithmetic operators to perform calculations. The five operators at N5 are:

OperatorSymbolExampleResult
Addition+12 + 517
Subtraction-12 - 57
Multiplication*12 * 560
Division/12 / 43.0
Exponentiation^2 ^ 8256

Arithmetic operators are processed by the ALU (Arithmetic Logic Unit) inside the processor. The computer always evaluates the right-hand side of an assignment before storing the result.

SET area TO length * breadth
SET total TO price + (price * 0.2)

BODMAS — operator precedence

When an expression contains more than one operator, the order in which they are calculated matters. Python and SQA reference language follow the same mathematical rule: BODMAS.

LetterStands forOperations
BBrackets( ) — always evaluated first
OOrders^ (exponentiation)
DDivision/
MMultiplication*
AAddition+
SSubtraction-

This means multiplication and division are always performed before addition and subtraction, unless brackets override the order.

Common BODMAS trap:

SET average TO num1 + num2 + num3 / 3   ← WRONG (divides num3 by 3 first)
SET average TO (num1 + num2 + num3) / 3  ← CORRECT (brackets force the addition first)

String concatenation

Concatenation means joining two or more strings together to form a longer string. At N5, the + operator is used for concatenation:

DECLARE firstName AS STRING INITIALLY ""
DECLARE surname AS STRING INITIALLY ""
SET firstName TO "Rohan"
SET surname TO "Singh"
SEND "Hello " + firstName + " " + surname + "!" TO DISPLAY

Output: Hello Rohan Singh!

Concatenation is also used to include variable values in output messages:

SEND "Your score is " + score + " out of 100." TO DISPLAY

At N5, the + operator is used to join each part of the output message in SEND statements.

Worked examples

Example 1 — Declaring variables for a quiz program

A program asks the user for their name and score (out of 50), then displays a personalised message.

DECLARE playerName AS STRING INITIALLY ""
DECLARE score AS INTEGER INITIALLY 0
DECLARE percentage AS REAL INITIALLY 0.0
SEND "Enter your name: " TO DISPLAY
RECEIVE playerName FROM (STRING) KEYBOARD
SEND "Enter your score (0-50): " TO DISPLAY
RECEIVE score FROM (INTEGER) KEYBOARD
SET percentage TO (score / 50) * 100
SEND playerName + " scored " + score + " out of 50 (" + percentage + "%)" TO DISPLAY
1
Three variables are declared: one string for the name, one integer for the score, one real for the percentage.
2
The player's name and score are received from the keyboard using matching data types.
3
percentage is calculated — BODMAS ensures score / 50 is done first (inside brackets), then the result is multiplied by 100.
4
The result is displayed using string concatenation with the + operator to build a single output line.
Example 2 — Area of a circle (exponentiation)

A program calculates the area of a circle using the formula area = π × r².

DECLARE radius AS REAL INITIALLY 0.0
DECLARE area AS REAL INITIALLY 0.0
DECLARE pi AS REAL INITIALLY 3.14159
SEND "Enter the radius: " TO DISPLAY
RECEIVE radius FROM (REAL) KEYBOARD
SET area TO pi * radius ^ 2
SEND "Area = " + area TO DISPLAY
1
All three variables are declared as real — radius and area can have decimal points, and pi is 3.14159.
2
The expression pi * radius ^ 2 is evaluated using BODMAS: Orders first (radius ^ 2 squares the radius), then multiplication by pi.
3
The result is stored in area and displayed using concatenation.
Example 3 — Choosing data types from a scenario

A cinema booking system stores the following information. Select the correct data type for each item.

Data itemData typeReason
Number of seats bookedIntegerA whole number — you cannot book half a seat
Ticket priceRealHas a decimal point (e.g. £8.75)
Customer's full nameStringText — a sequence of characters
Row letter (e.g. "G")CharacterA single letter only
Is the film sold out?BooleanCan only ever be TRUE or FALSE
1
Start by asking: is it text, or a number you'd never do arithmetic with? → String (or Character if it's a single symbol).
2
For numbers: does it have a decimal point? → Real. Is it always a whole number? → Integer.
3
If it can only be true or false → Boolean. This is the simplest type to spot once you know what to look for.
Now you try

A program calculates the total cost of ordering cinema tickets. The user enters the number of adult tickets (£9.50 each) and child tickets (£6.00 each).

Answer the following:

  1. Write a DECLARE statement for a variable called adultTickets that will store the number of tickets bought. Choose the most appropriate data type.
  2. Write an expression that calculates the total cost, storing it in a variable called totalCost. Assume adultTickets and childTickets have already been given values.
  3. Write a SEND statement that outputs: Total cost: £ followed by the value of totalCost.
  1. DECLARE adultTickets AS INTEGER INITIALLY 0 — Integer, because the number of tickets is always a whole number.
  2. SET totalCost TO (adultTickets * 9.50) + (childTickets * 6.00) — Brackets ensure each multiplication is done before the addition. totalCost should be declared as REAL.
  3. SEND "Total cost: £" + totalCost TO DISPLAY
Common mistakes
Using "float" instead of "real" in the exam. The SQA mark scheme awards marks for the term "real". Writing "float" or "decimal" will not gain the mark, even though they mean the same thing in other languages. Always write real.
Storing a numeric-looking value as an integer when it should be a string. Phone numbers, postcodes, and ID numbers look numeric but must be stored as strings. "EH14 4AP" contains letters and a space — it is impossible to store as an integer. If you would never do arithmetic with the value, it's a string.
Getting BODMAS wrong in an average calculation. SET avg TO a + b + c / 3 divides only c by 3, then adds the others — giving the wrong answer. Always use brackets: SET avg TO (a + b + c) / 3.
Confusing assignment and comparison. SET score TO 10 stores the value 10 in score. In an IF statement, IF score = 10 tests whether they are equal. These look similar but do completely different things.
Omitting the initial value in a DECLARE statement. DECLARE score AS INTEGER is incomplete in SQA reference language. The full form is DECLARE score AS INTEGER INITIALLY 0.
Exam tip

When asked to "describe" a data type, the SQA expects you to state what kind of data it holds. A complete description for integer is: "stores a positive or negative whole number (no decimal point)". One word like "number" will not gain the mark — it is too vague.

When asked to "choose a data type" for a given value, eliminate obviously wrong options first (is it text? → not integer/real/boolean), then check: does it have a decimal point? → real. Is it a whole number? → integer.

If a question shows a SEND statement and asks you to identify what it outputs, trace through any concatenated expressions carefully — the + operator joins each part left to right.

Task Set

Questions 1–5 are auto-checked. Questions 6–10 are self-marked — write your answer, then reveal the model answer to check your work.

1. Which data type would be most appropriate to store a player's high score of 14,500? TYPE 1

2. A variable stores the value 3.14159. Which data type is this? TYPE 1

3. Which line of code correctly declares a string variable in SQA reference language? TYPE 1

4. What is the output of the following code if name contains "Aisha" and score contains 37?
SEND "Well done " + name + "! You scored " + score TO DISPLAY TYPE 1

5. What is the result of the following expression: SET result TO 2 + 3 * 4? TYPE 1

6. A hospital patient record system stores the following data. For each item, state the most appropriate data type and give a reason. TYPE 2

(a) Patient's date of birth stored as "12/04/1985"
(b) Number of appointments attended this year
(c) Whether the patient has been discharged
(d) Ward temperature reading of 36.8°C

(a) String — contains slashes and separators; you would never do arithmetic with a date of birth.

(b) Integer — a whole number count; you cannot attend a fraction of an appointment.

(c) Boolean — can only be TRUE (discharged) or FALSE (not yet discharged).

(d) Real — contains a decimal point (36.8°C).

7. A program calculates the volume of a cuboid (length × breadth × height). Write the complete SQA reference language program including: declaring all variables, receiving inputs, calculating the volume, and displaying the result. TYPE 2

DECLARE length AS REAL INITIALLY 0.0
DECLARE breadth AS REAL INITIALLY 0.0
DECLARE height AS REAL INITIALLY 0.0
DECLARE volume AS REAL INITIALLY 0.0
SEND "Enter length: " TO DISPLAY
RECEIVE length FROM (REAL) KEYBOARD
SEND "Enter breadth: " TO DISPLAY
RECEIVE breadth FROM (REAL) KEYBOARD
SEND "Enter height: " TO DISPLAY
RECEIVE height FROM (REAL) KEYBOARD
SET volume TO length * breadth * height
SEND "Volume = " + volume TO DISPLAY

All four variables are real because dimensions may include decimal points. The formula multiplies all three dimensions in a single expression.

8. Explain the error in the following code and write the corrected version:
SET average TO score1 + score2 + score3 / 3 TYPE 2

Error: This is a BODMAS/operator precedence mistake. Division has higher precedence than addition, so score3 / 3 is evaluated first, then score1 and score2 are added to the result. This gives the wrong answer — it does not divide the sum of all three scores by 3.

Corrected: SET average TO (score1 + score2 + score3) / 3

The brackets force the addition to happen before the division.

9. Write a complete SQA reference language program for the following scenario: A taxi company charges £2.50 per mile. The user enters the distance of their journey in miles (which may include a decimal point). The program should calculate and display the total fare, and show a message stating whether the fare is over £20. Your answer should include: appropriate variable declarations, input, calculation, output with concatenation, and a conditional statement. TYPE 3

DECLARE distance AS REAL INITIALLY 0.0
DECLARE fare AS REAL INITIALLY 0.0
DECLARE pricePerMile AS REAL INITIALLY 2.50
SEND "Enter journey distance in miles: " TO DISPLAY
RECEIVE distance FROM (REAL) KEYBOARD
SET fare TO distance * pricePerMile
SEND "Distance: " + distance + " miles" TO DISPLAY
SEND "Total fare: £" + fare TO DISPLAY
IF fare > 20 THEN
    SEND "This fare is over £20." TO DISPLAY
ELSE
    SEND "This fare is £20 or under." TO DISPLAY
END IF

distance is real because the input may include a decimal point. Using pricePerMile as a named variable (rather than the literal 2.50) makes the code easier to maintain.

10. Trace through the code below step by step and state what is displayed on screen. Show your working. TYPE 3

DECLARE a AS INTEGER INITIALLY 5
DECLARE b AS INTEGER INITIALLY 3
DECLARE c AS REAL INITIALLY 0.0
SET c TO (a + b) ^ 2 / 4
SEND "Result: " + c TO DISPLAY

Step 1: (a + b) = (5 + 3) = 8 — brackets first (B in BODMAS)

Step 2: 8 ^ 2 = 64 — exponentiation next (O in BODMAS)

Step 3: 64 / 4 = 16.0 — division (D in BODMAS)

Step 4: c is assigned 16.0 (stored as real)

Output: Result: 16.0

Teacher notes — Shift+T to hide

Suggested timing: 60 minutes. Warm up (5 min) → vocab + intro (10 min) → data types notes with examples (15 min) → worked examples walkthrough (10 min) → Now You Try (5 min) → task set (15 min).

Exam question mapping:

  • Data types: Q1 (2024), Q5(a) (2024), Q7(a) (2025), Q12(b) (2025), Q2 and Q3 (2022), Q9(d) (2023) — appears almost every year, often 1–2 marks per data type identified.
  • Arithmetic / expressions: Q5(b) (2024), Q7(a) (2024), Q3(a) (2025), Q7(b) (2025) — commonly asks pupils to read and trace code or identify a bug.
  • Concatenation: embedded in most "write code" questions.

Key misconception to address: The BODMAS average trap (Q8 / Q3 type) appears every 1–2 years — worth 5 minutes of focused attention. "Real vs float" is a perennial 1-mark drop — stress it early and return to it.

Live demo suggestion: Type a short Python program live to show dynamic typing (no DECLARE needed), then contrast with the SQA reference language equivalent. Stress that Python syntax is not acceptable in the exam — SQA reference language only. Also worth demoing what happens when BODMAS is wrong for an average calculation.

Extension question: A variable x is declared as integer. What happens if the user enters 3.7? What about "hello"? Discuss type errors and why strict typing in SQA reference language is useful for catching bugs early.

Assignment relevance: SDD Task 1 requires a working program with correct data structures and calculations. Getting variable declarations and data types right is a prerequisite for all other implementation marks. String concatenation appears in required output formatting.

SQA command words covered: "identify" (data type), "describe" (data type), "explain" (operator precedence error), "write" (code).