Topic 8 Β· Programming Β· 8.2

Data Types Explained Simply

Data can be text, whole numbers, decimal numbers, single characters or true-and-false values. A data type tells the computer exactly what kind of value it is storing.

Invitation

Is every piece of data the same?

A name, an age and the price of a game are all pieces of data, but they are not the same kind of data.

A program needs to know whether a value is text, a whole number, a decimal number, one character or a true-and-false value.

A data type describes the kind of data being stored.
Figure 1
Different values need different types
"Amina" β†’ String
15 β†’ Integer
9.99 β†’ Real
TRUE β†’ Boolean
Big Idea

The data type controls how a value can be used

The computer uses the data type to decide how much memory may be needed and which operations make sense.

For example, two integers can be added mathematically. Two strings are normally joined as text instead.

Key idea: choose the type that matches the purpose of the value.
Figure 2
The same symbols can behave differently
12 + 3 β†’ 15
but
"12" + "3" β†’ "123"
Numbers calculate. Strings join.
FutureLogic Bridge

Data types are like labels on storage boxes

Imagine a storeroom filled with labelled boxes. One box is labelled whole numbers, another text, and another true or false.

The label tells you what belongs inside and how the contents should be handled. A data type does the same job inside a program.

Putting the wrong kind of item into a box can cause confusion. Storing the wrong data type can cause a program error or an incorrect result.

The label is the data type. The item inside is the value.
Figure 3
Label the box before storing the value
INTEGER box β†’ 42
STRING box β†’ "blue"
BOOLEAN box β†’ FALSE
Worked Example

Choose a suitable data type

A game stores a player's name, age, score average, first initial and whether the game is finished.

ValueSuitable data type
PlayerName   = "Maya"   β†’ STRING
Age          = 14       β†’ INTEGER
ScoreAverage = 87.5     β†’ REAL
Initial      = 'M'      β†’ CHAR
GameFinished = FALSE    β†’ BOOLEAN

Why these choices?

Age is an integer because it is stored as a whole number. ScoreAverage is real because it contains a decimal. GameFinished is Boolean because it has only two possible states.

Choose the type from the value's purposeβ€”not just how it looks.
The Five Core Types

Know what each type stores

INTEGERWhole numbers such as 7, 0 or βˆ’25.
REALNumbers containing a decimal, such as 3.5.
STRINGA sequence of characters, such as "Hello".
CHAROne character, such as 'A' or '?'.
BOOLEANOnly TRUE or FALSE.
RememberQuotation marks usually show text values.
Exam Tip

Use the most appropriate type

Exam questions often give a field or variable and ask for a suitable data type. Look closely at the values the program must store.

Telephone numbers, postcodes and identification codes are often strings because they are not used for arithmetic and may contain leading zeros or letters.

Use the exact programming term requested. For programming data, write STRING rather than a vague word such as β€œtext”.

Common Mistake

β€œIt contains digits, so it must be an integer.”

Digits can still form a string. The value "00127" may be an identification code, not a number used in a calculation.

Incorrect answer: β€œA phone number should be stored as an integer.”
Correct answer: β€œA phone number should usually be stored as a string because it is not used in calculations and may begin with zero.”
Figure 4
Appearance is not purpose
"01234" β†’ String
β‰ 
1234 β†’ Integer
Summary

What should you remember?

1A data type describes the kind of value stored.
2The main types are integer, real, string, char and Boolean.
3Choose the type that matches how the data will be used.
The correct data type helps the program store and process data correctly.