Data types in programming
A data type defines what kind of data a variable can hold and what operations are valid on it. OCR J277 requires you to identify, use and justify appropriate data types. Questions may ask you to identify types in given code, or to state the correct type for a given scenario.
The five main data types (OCR J277)
1. Integer
- Stores whole numbers (no decimal point): ..., -2, -1, 0, 1, 2, 100, -500.
- Operations: +, -, *, DIV (integer division), MOD (remainder).
- Examples of use: age, number of items, a loop counter, a score, a year.
age ← 16
count ← 0
score ← 100
2. Real (float/double)
- Stores numbers with a decimal point: 3.14, -0.5, 100.0, 2.718.
- Operations: +, -, *, / (and all standard arithmetic).
- Examples of use: a price, a temperature, a distance, a percentage, a weight.
price ← 4.99
temperature ← 36.6
Note: integers are a subset of real numbers, but storing whole numbers as integers is more memory-efficient and avoids floating-point rounding errors.
3. Boolean
- Stores only True or False (one of two values).
- Used in conditions and logical expressions.
- Examples of use: isLoggedIn, hasWon, isPrime, doorOpen.
isLoggedIn ← False
hasWon ← True
4. Character (Char)
- Stores a single character: a letter, digit, punctuation mark or symbol.
- Enclosed in single quotes in many languages.
- Examples: 'A', '3', '?', ' ' (space).
grade ← 'A'
initial ← 'H'
5. String
- Stores a sequence of characters (text of any length, including spaces).
- Enclosed in double quotes in OCR pseudocode.
- Examples of use: a name, a message, a password, an address, a postcode.
name ← "Alice"
message ← "Enter your PIN: "
postcode ← "SW1A 1AA"
Choosing the right data type
| Scenario | Correct type | Reason |
|---|---|---|
| Number of students in a class | Integer | Whole number; can't have 0.5 students |
| Average exam score | Real | May be a decimal (e.g. 68.4) |
| Whether a user has admin rights | Boolean | Only True or False |
| First letter of a student's surname | Character | Single character only |
| A student's full name | String | Multiple characters |
| A phone number (stored, not calculated) | String | Contains leading zeros and spaces; arithmetic not needed |
The phone number trap
- "07700 900123" contains a leading zero and a space.
- If stored as Integer, the leading zero is lost: 7700900123 (wrong) and spaces are not allowed.
- Phone numbers, postcodes, and National Insurance numbers should be stored as String, not Integer.
Type casting / conversion
Sometimes you need to convert between types:
- String to integer:
INT("42")→ 42. - Integer to string:
STR(42)→ "42". - String to real:
FLOAT("3.14")→ 3.14. - Character to ASCII code:
ASC('A')→ 65.
Common OCR exam mistakes
- Choosing Integer for prices — prices have decimals → use Real.
- Choosing Integer for phone numbers — leading zeros are lost → use String.
- Confusing Character and String — Character is exactly one character; String is zero or more characters.
- Forgetting Boolean can only be True or False — it cannot store "yes"/"no" (those are strings).
AI-generated · claude-opus-4-7 · v3-ocr-computer-science