Implementation: Data types and structuresStoring numbers

Data is stored differently depending on its type. Numbers are stored as integers or real numbers, text as string or characters. Lists of the same type of data can be stored in an array.

Part ofComputing ScienceSoftware design and development

Storing numbers

Integers

The following code is used to create an INTEGER variable that will be used to store an age.

DECLARE age AS INTEGER INITIALLY 0
SEND “Enter your age” TO DISPLAY
RECEIVE age FROM KEYBOARD

The user is asked to enter their age and when they type in a number, the value is stored in the 'age' variable.

An integer is a whole number - a number without decimal places.

Integers can be positive or negative.

Examples of integers:

  • 24
  • -36
  • 4303
  • -89730
  • 7903493
  • 0

This program will only accept a whole number. Anything else will cause an error. Here's that line of code running:

Displaying a user's age on a laptop screen.

When the number 45 is entered the program works fine. However, here's the same line of code running with something different entered:

Coding behind the user's age.

Here the user entered the word 'old' when they were asked for their age. The program was expecting an integer (a whole number) and therefore shows an error.

To store a whole number, use the data type known as an 'integer'.

Real numbers

Numbers that contain a decimal point are stored in a data type called REAL.

Variables that use the real data type can accept both positive and negative numbers with a decimal place.