Variables, constants and data types Cambridge IGCSE Computer Science (9–1) revision

Not started

Learn it

In plain words

A program has to remember things as it runs: a score, a name, whether the game is over. Each is kept in a named place in memory. If the value may change, the place is a variable. If it must never change, it is a constant.

Every one has a data type, which says what kind of value it holds and so what can be done with it.

6 things to know

  1. A variable is a named store in memory whose value can change while the program runs. A constant is a named store whose value cannot change while the program runs.
  2. Declaring a variable: DECLARE Age : INTEGER. Declaring a constant: CONSTANT TaxRate ← 0.2.
  3. INTEGER: a whole number, such as 17 or −3. REAL: a number with a fractional part, such as 3.75.
  4. CHAR: a single character, such as 'M'. STRING: a sequence of characters, such as "Hello". BOOLEAN: TRUE or FALSE.
  5. Assignment gives a variable a value: Age ← 16. INPUT Name reads a value from the user. OUTPUT "Hello ", Name displays text and a value.
  6. Give every variable and constant a meaningful name, such as TotalCost, not T.

Worked example

A program stores a person's name, age, height in metres, and whether they hold a ticket. Choose a data type for each.

  1. The name is a sequence of characters: STRING.
  2. The age is a whole number: INTEGER.
  3. The height, such as 1.62, has a fractional part: REAL.
  4. Holding a ticket is yes or no: BOOLEAN.

Tips and tricks

  • A phone number is a STRING, not an INTEGER. No arithmetic is done with it, and as a number it would lose its leading zero.
  • A constant makes a program easier to maintain: if the value changes, it is changed in one place only.
6 questions, about 2 minutes.

It lands in your notebook with its questions as flashcards.

Variables, constants and data types: 6 questions and answers

These are the quiz’s questions. Do the quiz first, then come back here for the ones that got you.

  1. Which data type would store the value 3.14?
    • INTEGER
    • REAL (the answer)
    • CHAR
    • BOOLEAN

    It has a fractional part.

  2. Which data type stores only TRUE or FALSE?
    • STRING
    • CHAR
    • BOOLEAN (the answer)
    • INTEGER

    Two possible values.

  3. Which data type is best for a single letter such as a grade 'B'?
    • CHAR (the answer)
    • STRING
    • REAL
    • BOOLEAN

    CHAR holds one character.

  4. Which data type should be used for a telephone number?
    • INTEGER
    • REAL
    • STRING (the answer)
    • BOOLEAN

    No calculations are done with it, and a leading zero must be kept.

  5. What is a constant?
    • a variable that is never used
    • a named value that cannot change while the program runs (the answer)
    • a number that is very large
    • a kind of loop

    Such as the number of days in a week.

  6. What does DECLARE Score : INTEGER do?
    • outputs the score
    • creates a variable called Score that holds whole numbers (the answer)
    • sets Score to zero
    • inputs a number

    It names the variable and gives its data type.

Still stuck on this one?Ask in the Papermunch Discord, or help someone else who is. Discord is for ages 13 and up.Join the server

Things you can type

Or go straight to

Or browse a shelf