The basic data types are Boolean, char, integer, string, and decimal. Each determines what kind of value a variable can store and which operations can validly be performed on that value.
In B2.1 Programming fundamentals, a data type defines the possible values of a variable, how those values are represented, and the operations that can be applied to them.
| Data type | Purpose | Example values |
|---|---|---|
| Boolean | Stores one of two logical values. It is commonly used in conditions, selection, and iteration. | true, false |
| char | Stores one character, such as a letter, digit, punctuation mark, or space. | 'A', '7', '?' |
| integer | Stores a whole number without a fractional part. | -12, 0, 45 |
| string | Stores a sequence of characters, including spaces and punctuation. | "IB Computer Science", "A7" |
| decimal | Stores a number that may contain a fractional part. Depending on the language, this may be called a real, floating-point, or decimal type. | 3.14, -0.5, 12.0 |
For example, a program could declare age as an integer, name as a string, and isRegistered as a Boolean. The expression age + 1 is valid because arithmetic can be performed on integers, while isRegistered + 1 is normally invalid because a Boolean is not a numerical value.
A common misconception is that a char and a string are interchangeable. A char contains exactly one character, whereas a string contains a sequence of zero or more characters. Similarly, "25" is a string and cannot automatically be treated as the integer 25 without type conversion.
For an IB exam response, identify the data type and justify it using the value being stored. When tracing code, pay close attention to quotation marks, fractional values, and Boolean conditions because these often reveal the intended type.