💻 Component 2 · 2.2 Programming
2.2.1a Variables, Constants & Operators
OCR J277 · GCSE Computer Science · ~10 min read
Notes
Video
Slides
Worksheet
Quiz

Variables

A variable is a named storage location in memory whose value can change while the program runs. Variables are declared with a name and assigned a value using the assignment operator (= or in pseudocode).

// Declaring and assigning variables
score = 0           // integer variable
name = "Alice"     // string variable
passed = True      // Boolean variable
score = score + 10  // value changes — this is fine for a variable

Variable names should be meaningful (e.g. totalScore not x). They cannot start with a number or contain spaces.

Constants

A constant is a named value that is set once and does not change during the program. In OCR J277 pseudocode, constants are declared using const.

const PI = 3.14159
const MAX_LIVES = 3
const GRAVITY = 9.81

Why use constants? They make code easier to read and maintain. If the value needs changing (e.g. VAT rate changes from 20% to 19%), you only change it in one place. They also prevent accidental changes to values that should never change.

Variable vs Constant — Key Difference

FeatureVariableConstant
Value can change?Yes — can be updated anytimeNo — set once at declaration
OCR pseudocodescore = 0const MAX = 10
Examplesscore, name, count, totalPI, MAX_SIZE, VAT_RATE
Use caseStoring values that change during executionFixed values used throughout program

Arithmetic Operators

OperatorNameDescriptionExample
+AdditionAdds two values5 + 3 = 8
-SubtractionSubtracts right from left10 - 4 = 6
*MultiplicationMultiplies two values6 * 7 = 42
/DivisionDivides left by right (can give decimal)7 / 2 = 3.5
DIVInteger divisionWhole number result, discards remainder7 DIV 2 = 3
MODModuloReturns the remainder after integer division7 MOD 2 = 1
^ExponentiationRaises to the power of2 ^ 3 = 8

Comparison Operators

OperatorMeaningExample (result)
==Equal to5 == 5 → True
!=Not equal to5 != 3 → True
<Less than3 < 7 → True
>Greater than9 > 4 → True
<=Less than or equal to5 <= 5 → True
>=Greater than or equal to6 >= 10 → False

Boolean (Logical) Operators

OperatorMeaningExample
ANDBoth conditions must be trueage >= 16 AND hasID == True
ORAt least one condition must be truescore > 50 OR bonus == True
NOTInverts the Boolean valueNOT locked → True if locked is False

String Operators

The + operator is used for string concatenation in OCR pseudocode — joining two strings together.

firstName = "Alice"
lastName = "Smith"
fullName = firstName + " " + lastName
// fullName = "Alice Smith"
Exam tip: OCR J277 exams commonly test: (1) the difference between variables and constants, (2) DIV vs MOD (know these cold — they appear in trace tables too), (3) Boolean operators in IF conditions. When a question says "explain the purpose of a constant", always mention: set once, doesn't change, improves maintainability.
⚠️ Common Mistakes
  • Confusing = (assignment) with == (comparison) — they are different operators
  • Saying a constant is the same as a variable — they are NOT; a constant cannot change
  • Getting DIV and MOD mixed up: DIV = whole number result, MOD = remainder
  • Using + to add a number and a string — this causes a type error; you must convert first
  • Forgetting that AND requires BOTH conditions to be true; OR only needs ONE
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.2.1a Variables, Constants & Operators

8 questions · 20 marks

Q1State the difference between a variable and a constant.[2]
✅ Mark scheme
A variable is a named memory location whose value can change during program execution [1]. A constant is a named value that is set once and cannot be changed during program execution [1].
Q2Give two reasons why a programmer would use a constant instead of a variable.[2]
✅ Mark scheme
Any two: prevents accidental changes to the value [1]; makes code easier to read and understand (self-documenting) [1]; if value needs updating, only change it in one place (maintainability) [1]; makes code easier to debug [1].
Q3Evaluate: (a) 17 DIV 5 (b) 17 MOD 5 (c) 2 ^ 4 (d) 9 MOD 3[4]
✅ Mark scheme
(a) 17 DIV 5 = 3 [1] (17 = 5×3 + 2). (b) 17 MOD 5 = 2 [1]. (c) 2^4 = 16 [1]. (d) 9 MOD 3 = 0 [1] (9 divides exactly by 3).
Q4What is the result of each Boolean expression? (a) True AND False (b) True OR False (c) NOT True (d) False OR False[4]
✅ Mark scheme
(a) False [1] (b) True [1] (c) False [1] (d) False [1]. AND requires both; OR requires at least one; NOT inverts.
Q5Write pseudocode to declare a constant called VAT_RATE with a value of 20, and a variable called price with a value of 50. Calculate and store the vat amount.[3]
✅ Mark scheme
const VAT_RATE = 20 [1]; price = 50 [1]; vatAmount = price * VAT_RATE / 100 [1] (or equivalent valid expression).
Q6What does the comparison operator == do? How is it different from = ?[2]
✅ Mark scheme
== tests whether two values are equal and returns True or False [1]. = is the assignment operator — it assigns a value to a variable (e.g. x = 5 stores 5 in x) [1]. They serve completely different purposes.
Q7A student writes: score = "100" + 50. Explain why this might cause an error.[2]
✅ Mark scheme
"100" is a string (text) [1]; 50 is an integer. You cannot add a string and an integer with + — this causes a type error. The string "100" must first be converted to an integer [1].
Q8Write a Boolean expression in pseudocode that is True only when a student's score is at least 40 AND they have not been absent.[1]
✅ Mark scheme
score >= 40 AND NOT absent [1] (or: score >= 40 AND absent == False). Award mark for correct use of >= and NOT/AND.
?
out of 20 — self-mark above
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 10
Click to reveal definition
🎉
Complete!
TermDefinition
🎯

Mini Test — 2.2.1a Variables, Constants & Operators

10 questions · 10 marks · 10 minutes

← 2.1.3e Trace Tables 2.2 Programming 2.2.1b Data Types →