GCSE Computer Science · public explanation

Variables, Data Types and Operators

Understand variables, data types and operators in GCSE programming. Follow how values are stored, calculated and compared in worked examples.

Read the explanation and worked examples, then choose your exam board to continue with activities and practice in your account.

OCR J277On the specificationOCR J277 2.2.1–2.2.3

Apply variables, data types and operators using OCR Exam Reference Language or a studied language only when requested.

AQA 8525 (2027+)On the specificationAQA 8525 (2027+) 3.2.1–3.2.5, 3.2.7, 3.2.9

Apply variables, data types and operators in the precise response format printed on the paper.

From input to a stored value

A variable names a storage location whose value may change. A named constant holds a value that the program is not intended to change. Choose a suitable data type and follow the declaration or assignment notation used in the question. Integer stores whole numbers, real can represent fractional values, Boolean stores true or false, character stores one character and string stores a sequence of characters.

Input obtains a value from outside the program, such as the keyboard. Assignment evaluates the right-hand expression and stores its result in the named variable. Output sends a result to the display. A displayed number is not automatically saved between runs.

Worked example: tickets

  1. Set the constant ticket price to 4.50 and obtain an integer quantity of 3.
  2. Calculate total = 3 × 4.50 = 13.50; total needs a real data type.
  3. Output 13.50. Changing only quantity to 4 leaves the constant price, stored total and display unchanged. Recalculate total to store 18.00, then execute output again to update the display.

Use the type that fits the meaning: a phone number may contain digits but is normally text because arithmetic on it is not meaningful.

Choose the type from what a value means

A data type describes the kind of value a program can work with and which operations make sense. An integer is a whole number, including zero and negative whole numbers. A real value can have a fractional part; it can also represent a whole-number value such as 4.0. Boolean means True or False, not the quoted text 'False'.

A character is one character, such as a letter, digit or punctuation mark. A string is a sequence of characters and can be empty or only one character long. When asked for the most specific type for exactly one letter, choose character. Digit characters in an identifier need not be numbers used for arithmetic: converting '0074' to integer 74 loses its original leading zeroes.

A variable has a name used to access its current value. A named constant gives a meaningful name to a value that must stay fixed during the run. A declaration introduces a name; an initial assignment supplies a value. Some languages require a separate typed declaration; others introduce a variable on assignment. Follow the notation printed in your exam question rather than inventing a declaration syntax. A name that has not yet been assigned a value is not automatically zero, False or an empty string.

Value in a cinema systemSuitable typeWhy
Number of available seats: 0IntegerA whole-number count; zero is a valid stored value
Price that may include pence: 6.50RealFractional values must be possible
Booking confirmed: FalseBooleanOne of two logical states
Row label: BCharacterExactly one letter is required
Booking identifier: 0074StringAll four digit characters, including leading zeroes, matter

Choose the calculation the question actually asks for

Addition increases a total, subtraction finds a difference and multiplication combines equal groups. Real division / keeps a fractional quotient. For the positive whole-number examples here, DIV counts complete groups and MOD counts the items left over. These are different requested results, not alternative ways to format the same answer.

Evaluate brackets first, then multiplication and division before addition and subtraction. Operators of the same precedence are evaluated from left to right in these examples. A final context decision may follow the calculation: if a remainder of passengers is non-zero, another vehicle is needed. If the remainder is zero, do not add an unnecessary vehicle. OCR's separate casting and powers section covers its additional exponentiation requirement.

ExpressionResultInterpretation
8 + 3 * 214Multiply 3 by 2 before adding 8
(8 + 3) * 222Brackets change the order
20 - 3 - 215Subtract left to right
22 / 54.4Real quotient
22 DIV 54Four complete groups of five
22 MOD 52Two items left over
20 MOD 50No items remain

A comparison produces a Boolean, not an assignment

Relational operators compare two values: equal to, not equal to, less than, greater than, less than or equal to, and greater than or equal to. The result is True or False. The inclusive comparisons include equality: five is at most five, but five is not less than five. The table uses words so neither board's assignment symbol is mistaken for a comparison.

AND is True only if both conditions are True. OR is inclusive: it is True if either condition or both conditions are True. NOT reverses a Boolean. Work out the bracketed comparisons before combining them. AQA and OCR programming conditions require AND, OR and NOT here; the separate Boolean-logic topic handles any board-specific extra gates.

With count 5 and ready FalseResultReason
count is equal to 5TrueThe values match
count is not equal to 5FalseThe values match
count is less than 5FalseEquality does not satisfy a strict comparison
count is greater than 5FalseFive is not greater than itself
count is at most 5TrueThe upper limit includes equality
count is at least 5TrueThe lower limit includes equality
(count is at most 5) AND readyFalseTrue AND False
(count is equal to 5) OR readyTrueTrue OR False
NOT readyTrueNOT False

Continue with your exam board

The course combines lesson videos, interactive activities, topic questions and original exam mocks. Sign in to use the learning tools and save your progress; your account’s course access applies.

Independent learning resources. Not endorsed by AQA or OCR. These public introductions complement the full course and the official specification.