Data type concept
What is a Data Type?
A data type tells a program what kind of data a variable holds and what operations can be performed on it. Every value stored in a program has a data type - whether it is a whole number, a decimal, a piece of text, or a true/false value. Choosing the correct data type matters: it affects how much memory is used, what calculations are possible, and how the data behaves in the program.
Common Data Types
An integer is a whole number - positive, negative, or zero. No decimal point is stored. Integers are used for counting, indexing, and any value that is naturally whole.
Examples: age of a person (17), score in a game (342), number of items in a list (0).
Operations allowed: arithmetic (+, -, ×, ÷), comparison (<, >, =).
A real (also called float or floating point) is a number that can have a decimal part. It is used when precision beyond whole numbers is needed.
Examples: temperature (36.6), price (£4.99), average score (7.3).
Operations allowed: arithmetic (+, -, ×, ÷), comparison. Note: real arithmetic can introduce tiny rounding errors.
A string is a sequence of characters - letters, digits, spaces, and symbols. Strings are always enclosed in quotation marks in code. A digit stored as a string (e.g. "7") cannot be used in arithmetic without conversion.
Examples: a name ("Alice"), a postcode ("SW1A 1AA"), a message ("Game over").
Operations allowed: concatenation (joining), length, comparison, sub-string extraction.
A Boolean stores one of only two possible values: TRUE or FALSE. Booleans are used to represent yes/no conditions and to control the flow of a program.
Examples: whether a user is logged in (TRUE), whether a game is over (FALSE).
Operations allowed: logical operations (AND, OR, NOT), comparison.
Key Takeaways
- A data type defines what kind of value a variable holds and what can be done with it.
- The four main data types are integer, real, string, and Boolean.
- Choosing the wrong data type can prevent valid operations - a digit stored as a string cannot be used in arithmetic directly.
- A Boolean holds only TRUE or FALSE and is used to represent conditions and decisions in a program.