1. Core Data Types in Programming
A data type is a classification that tells the computer how to interpret a value. It defines what kind of data a variable can hold (e.g., whole numbers, text, true/false values) and what operations can be performed on that data. Choosing the correct data type is crucial for writing efficient and error-free programs. The most common fundamental types are Integer, Real, Character, String, and Boolean.
Key term
Examiner insight
Common pitfall
Worked example 15 marks
A program needs to store details for a library book. For each piece of information below, choose the most appropriate data type from Integer, Real, String, Char, or Boolean. Justify your choice for each.
- Title
- Number of pages
- Price
- Is it currently on loan?
- Shelf location code (e.g., 'F')
- 1
- Title: String. A book title consists of a sequence of characters, letters, and possibly numbers, which is the definition of a string.
- 2
- Number of pages: Integer. The number of pages will always be a whole number.
- 3
- Price: Real. Price can include decimal values (e.g., 9.99), so a Real (or float) is required to store the fractional part.
- 4
- Is it currently on loan?: Boolean. This is a simple yes/no or true/false question, which is exactly what the Boolean data type is for.
- 5
- Shelf location code: Char. Since the example 'F' is a single character, the Char data type is the most memory-efficient choice.
Recap
- An Integer stores whole numbers (e.g., -5, 0, 100).
- A Real (or Float) stores numbers with decimal points (e.g., 3.14, -0.05).
- A Char stores a single character (e.g., 'A', '7', '$').
- A String stores a sequence of characters (e.g., "Hello World").
- A Boolean stores one of two values: True or False.
- Choosing the right data type prevents errors and saves memory.
Quick check
- What data type would you use to store a person's age in years?1 mark
- Which data type is most suitable for storing the average mark in a test, which was 72.5?1 mark