6.2 - Data Types, Operators & Variables
Main data types in programming
Programming involves working with different kinds of data, and each type is stored in a specific way to ensure the computer handles it correctly. Data types define what kind of information is being used and how it can be processed.
The five main data types
| Data type | Characteristics | Examples |
|---|---|---|
| Integer | Whole numbers without any decimal parts, including positive, negative, or zero. | 0, 6, 10293, -999 |
| Real (or float) | Numbers that include a decimal part, allowing for fractions or precise measurements. | 0.15, -5.87, 100.0 |
| Boolean | Limited to just two possible values, often used for true/false decisions. | True/False, 1/0, yes/no |
| Character | A single letter, digit, or symbol, typically enclosed in single or double quotes. | 'A', 'k', '5', '_', '$' |
| String | A sequence of characters forming words or longer text, also enclosed in quotes. | "Hello World!", "MnC41B" |
Choosing the correct data type
Selecting the right data type is essential because it affects how a program runs and processes information. If you use the wrong type, the program might not work as expected or could produce errors.
Example data types for a registration form
| Field | Example value | Appropriate data type |
|---|---|---|
| Initial of first name | S | Character |
| Surname | Black | String |
| Age | 32 | Integer |
| Weight (in kg) | 81.4 | Real |
| Are you scared of heights? (Yes or No) | Yes | Boolean |
Operators in programming
Operators are symbols that perform specific actions in a program, such as calculations or comparisons.
Arithmetic operators
Arithmetic operators take two numbers and perform mathematical calculations on them. They work on integers, reals, or a mix of both. They will be carried out in the order of BODMAS (Brackets, Other (like powers), Division and Multiplication, Addition and Subtraction). For example, in the expression 6 + 4 / 2, the division operator happens before the addition operator, resulting in 8.
| Function | Operator | Example | Result |
|---|---|---|---|
| Addition | + | 4 + 7 | 11 |
| Subtraction | - | 6 - 8 | -2 |
| Multiplication | * | 7 * 3 | 21 |
| Division | / | 20 / 5 | 4 |
| Exponential (powers) | ^ or ** | 23 | 8 |
Comparison operators
Comparison operators check how two values relate and return a Boolean result (True or False). Note that symbols might vary slightly in different programming languages.
| Operator | Meaning | Evaluates to True | Evaluates to False |
|---|---|---|---|
| Is equal to | 5 5 | 5 == 8 | |
| != or <> | Is not equal to | 6 != 7 | 6 != 6 |
| < | Is less than | 4 < 10 | 3 < 2 |
| > | Is greater than | 15 > 9 | 10 > 12 |
| <= | Is less than or equal to | 7 <= 8 | 11 <= 10 |
| >= | Is greater than or equal to | 3 >= 3 | 9 >= 12 |
Boolean operators
Boolean operators work with True and False values, producing another Boolean result. There are three Boolean operators: AND, OR, and NOT, and they are similar to logic gates in computing.
| Operator | Evaluates to True if: | Evaluates to False if: |
|---|---|---|
| AND | Both inputs are True (e.g., 3 < 5 AND 2 > 1) | Either input is False (e.g., 4 < 5 AND 10 > 20) |
| OR | Either input is True (e.g., 1 > 8 OR 2 == 2) | Both inputs are False (e.g., 1 == 8 OR 2 < 2) |
| NOT | The input is False (e.g., NOT(5 > 8)) | The input is True (e.g., NOT(10 > 6)) |
Variables and constants
Variables and constants are ways to store data in a program, but they differ in how and when their values can be changed.
Variables
A variable is a named storage location for data that can change during program execution. The assignment operator = sets or updates a variable's value, with the variable name on the left and the value on the right. Do not confuse this with the comparison operator ==, which checks for equality.
Example of variables in pseudocode:
Constants
A constant is similar to a variable but its value is set before the program runs and cannot be changed during execution. Attempting to alter a constant might cause an error.
Changing data types with casting
Casting allows you to convert a value from one data type to another. Common casting functions include INT() for integer, REAL() for real, and STR() for string.
Example of casting in pseudocode
Note that tempA remains an integer after these operations, as casting creates a new value without altering the original.