So far we have only considered variables that represent numbers. But we can also use the computer's memory to store other types of data such as letters and characters like 'a', '?', or 'Z'. To store these types of data we will also need to create variables, but our variables should be different from the variables we use to store numbers. Consider the following problem. What will happen if we give the computer an assignment operation like this:
|TotalPrice := 23.50 + 'c'|
Clearly this instruction makes no sense. The variable TotalPrice was intended to contain numbers, not letters! What we need is a way of specifying what type of data a given variable should contain. This is exactly the idea of data types.
In high-level programming languages, each new variable is associated with a certain type such as integer or character. The type of the variable defines what kind of data the variable can hold and what kinds of operations are appropriate for that data. In our example statement above, it is clearly not appropriate to add the letter 'c' to the number 23.50. By specifying the type of a variable when it is created, the computer can make sure our instructions are valid before trying to execute them. A typical way of specifying the type of a variable is to write the type name before the variable identifier like this:
NumberOfShoes := 10
Character SomeLetter := 't'
Most programming languages have a set of basic data types that includes integer, real, character, and Boolean variables. These languages also typically allow us to manipulate groups of adjacent memory cells that have the same type. These groups are called arrays. To learn more about each data type and the operations they can perform, click the labels in the animation below.