Study Guide

Variables, Data Types and Operators

How a program remembers values: variables and constants, the five basic data types, input and output, instructions carried out in sequence, and the arithmetic, relational and logical operators, all in Cambridge pseudocode with Python alongside.

Lesson 5.3 Algorithms and Programming

For: Complete beginners to programming: Key Stage 3 (about ages 11 to 14), or a Cambridge IGCSE Computer Science course

Stage: Think

Remembering a score

In a fictional game called Meteor Dash, the player’s score starts at zero and goes up by 10 each time they collect a coin. While the game is running, the computer has to remember the score from one moment to the next.

Think it through

What must the computer be able to do with that remembered score? And what would you call the place where it is kept?

Show a model answer

It must be able to read the score (to show it), and to change it (to add 10). It also needs a name for the place, so that the program can say which value it means. In programming, a named place that holds a value which can change is called a variable.

Stage: Predict

Variables and assignment

A variable is a named place in the computer’s memory that holds one value, which the program can change. Storing a value in a variable is called assignment, and in Cambridge pseudocode it is written with an arrow, ←. Here is Meteor Dash’s score being updated twice:

Score ← 0
Score ← Score + 10
Score ← Score + 10
OUTPUT Score

Think it through

Predict what this program outputs, and explain what the second line does. Note that Score appears on both sides of the arrow.

Show a model answer

It outputs 20. Line 1 stores 0. In line 2 the computer works out the right-hand side first: the current value of Score (0) plus 10 is 10, and that is stored back in Score, replacing the old value. Line 3 does the same again, giving 20.

The rule: the right-hand side is worked out first, using the values the variables hold now, and the result is stored in the variable on the left, replacing what was there. Follow the program in a trace table. A blank cell means that nothing changes or nothing is output on that line.

Following the Meteor Dash score
Line carried outScoreOUTPUT
Score ← 0
Score ← Score + 10
Score ← Score + 10
OUTPUT Score

    Show the completed table
    Line carried outScoreOUTPUT
    Score ← 00
    Score ← Score + 1010
    Score ← Score + 1020
    OUTPUT Score20

    In mathematics, Score = Score + 10 can never be true. In pseudocode, Score ← Score + 10 is not an equation: it is an instruction that means “take the current value of Score, add 10 and store the answer in Score”. The arrow works in one direction only: Score ← 5 stores 5 in Score, but 5 ← Score means nothing.

    Check your understanding

    After these three lines have run, what does Total hold? Total ← 5, then Total ← Total * 2, then Total ← Total + 1.
    Show the answer

    Answer: C. 11

    Each assignment replaces the old value with the new one.

    Stage: Discover

    Constants, declarations and names

    The game awards 10 points for a coin, and the number 10 is typed into forty different places in the code. The designers decide a coin should be worth 15.

    Think it through

    What problem will the designers have, and how could the program have been written to avoid it?

    Show a model answer

    They would have to find and change all forty places, and missing one would make the game give different points in different places. It would be better to give the value a name once, and use the name everywhere. Then it is changed in one place only.

    A value that is set once and never changes while the program runs is a constant. A variable’s value is meant to change; a constant’s is not. In Cambridge pseudocode:

    CONSTANT PointsPerCoin ← 10
    DECLARE Score : INTEGER
    DECLARE PlayerName : STRING
    Score ← Score + PointsPerCoin
    • CONSTANT gives a name to a fixed value. Its value must be written out as a plain value (a literal such as 10 or “N/A”): it cannot be a variable or a calculation.
    • DECLARE says that a variable exists, and which data type it holds (the next step).
    • The names the programmer chooses are called identifiers. They use letters and digits only, start with a capital letter, and use a capital for each new word (NumberOfPlayers). There are no spaces or underscores, and they should describe what they hold. A keyword such as OUTPUT cannot be a name.

    Check your understanding

    Which of these is an acceptable identifier in Cambridge pseudocode?
    Show the answer

    Answer: A. TotalToPay

    Good names describe what the value is, so the program can be read like a description.

    Check your understanding

    Why is a constant such as PointsPerCoin better than typing 10 wherever the score changes?
    Show the answer

    Answer: B. The value can be changed in one place, and the name shows what the number means.

    Constants make a program easier to read and to update.

    Stage: Discover

    Kinds of value

    Here are ten values: 42, 3.5, "Ali", 'A', TRUE, "42", -7, 0.0, "Hello World", FALSE.

    Think it through

    Sort these ten values into groups of the same kind of thing. How many groups do you make, and what would you call each?

    Show a model answer

    One sensible grouping is five groups: whole numbers (42, -7); numbers with a fractional part (3.5, 0.0); text (“Ali”, “42”, “Hello World”); one single character (‘A’); and true-or-false values (TRUE, FALSE). Notice that "42" has quotation marks, so it is text, not a number, even though it looks like one.

    Every value has a data type: the kind of value it is. The type decides what the program can do with it, such as add it, join it to other text or test whether it is true. The syllabus uses five basic types:

    Data typeHoldsWritten asExample variable
    Integer (INTEGER)A whole number, positive, negative or zero5, -3NumberOfLives
    Real number (REAL)A number that can have a fractional part, always written with a digit on both sides of the point4.7, 0.3, -4.0, 0.0Height
    Char (CHAR)One single character, in single quotes'x', '@'Grade
    String (STRING)Zero or more characters, in double quotes; "" is the empty string"Ali", "Hello World"PlayerName
    Boolean data type (BOOLEAN)Only TRUE or FALSETRUE, FALSEGameOver

    A variable is declared with its type, as in DECLARE Score : INTEGER, and then holds only values of that type.

    A number written in quotation marks is text. "42" is a STRING: the computer sees two characters, a 4 and a 2, and cannot add them. 42 is an INTEGER. Use a number type when the value will be added, compared or averaged, and STRING for anything that is only ever displayed or stored as text.

    Check your understanding

    A school office stores each pupil’s phone number, such as 07700 900123, in a variable. Which data type should it use?
    Show the answer

    Answer: D. STRING, because it is a label, not a quantity, and the leading 0 must be kept.

    Choose the type from how the value is used, not from how it looks.

    Choose the best type for each variable in a fictional game’s code.

    Choosing the best data type for each variable
    Variable and what it holdsData type
    NumberOfLives: 3
    PlayerName: "Zara"
    Height: 1.62 (metres)
    GameOver: TRUE
    Grade: ’B’
    PostCode: "LS1 4AB"

      Show the completed table
      Variable and what it holdsData type
      NumberOfLives: 3INTEGER
      PlayerName: "Zara"STRING
      Height: 1.62 (metres)REAL
      GameOver: TRUEBOOLEAN
      Grade: ’B’CHAR
      PostCode: "LS1 4AB"STRING

      Stage: Predict

      Input, output and sequence

      A program gets values from the user with INPUT and shows values with OUTPUT. INPUT Name waits for the user to type and stores what they typed in the variable Name. OUTPUT can show several values in one go, separated by commas: they are joined together with nothing added between them, so any spaces must be put inside the quotation marks. Here is a short fictional program:

      DECLARE Name : STRING
      DECLARE Age : INTEGER
      OUTPUT "What is your name?"
      INPUT Name
      OUTPUT "How old are you?"
      INPUT Age
      Age ← Age + 1
      OUTPUT Name, " will be ", Age, " next year"

      Think it through

      A user types Maya and then 12. Write down everything the program shows on the screen, in order.

      Show a model answer

      The screen shows three lines:

      What is your name?
      How old are you?
      Maya will be 13 next year

      The Age that was typed is 12; line 7 adds 1 and stores 13, and the last line joins Name, the text, Age and the text with nothing added between them.

      Instructions are carried out one after another, in the order they are written: this is called sequence. Check your prediction with a trace table (the user types Maya, then 12).

      Following the greeting program
      Line carried outNameAgeOUTPUT
      OUTPUT "What is your name?"
      INPUT Name
      OUTPUT "How old are you?"
      INPUT Age
      Age ← Age + 1
      OUTPUT Name, " will be ", Age, " next year"

        Show the completed table
        Line carried outNameAgeOUTPUT
        OUTPUT "What is your name?"What is your name?
        INPUT NameMaya
        OUTPUT "How old are you?"How old are you?
        INPUT Age12
        Age ← Age + 113
        OUTPUT Name, " will be ", Age, " next year"Maya will be 13 next year

        Apply it

        A shop program runs: Price ← 8, then Price ← Price * 2, then Price ← Price - 1, giving 15. A programmer swaps the last two lines, so the order is Price ← 8, Price ← Price - 1, Price ← Price * 2. What is Price at the end?
        Show the answer

        Answer: C. 14, because 8 - 1 = 7 is stored first and then doubled.

        In a sequence the same instructions in a different order can give a different result.

        Stage: Discover

        Arithmetic operators

        An operator is a symbol or word that works on values to give a result. Start with a sharing problem.

        Think it through

        17 sweets are shared out equally between 5 children, and nobody may be given a part of a sweet. How many does each child get, and how many are left over?

        Show a model answer

        Each child gets 3 sweets (3 × 5 = 15), and 2 sweets are left over (17 - 15). Dividing like this, to a whole number and a remainder, is something programs do all the time.

        The arithmetic operators in the syllabus are:

        OperatorMeaningExampleResult
        +add7 + 29
        -subtract7 - 25
        *multiply7 * 214
        /divide (the result may have a fractional part)7 / 23.5
        ^raise to a power2 ^ 38
        DIVdivide and keep only the whole number of timesDIV(17, 5)3
        MODdivide and keep only the remainderMOD(17, 5)2

        DIV(17, 5) is the number of children’s sweets, and MOD(17, 5) is the number left over. When a calculation has several operators, powers are worked out first, then * and /, then + and -; brackets are worked out before everything else. So 2 + 3 * 4 is 14, but (2 + 3) * 4 is 20. Putting brackets in makes the order clear, even when they are not needed.

        1. Decide what is needed. Whole hours and the minutes left over. “Whole” and “left over” point to DIV and MOD.
        2. Hours. There are 60 minutes in an hour, so the whole hours are DIV(130, 60). 60 goes into 130 twice (120), so Hours is 2.
        3. Minutes. What is left after taking out the whole hours is MOD(130, 60): 130 - 120 = 10, so Minutes is 10.
        4. Check. 2 hours is 120 minutes, and 120 + 10 = 130. The answer rebuilds the original number, so it is right.

        Using / would have given 2.1666…, which does not say how many whole hours and minutes there are. That is why DIV and MOD are needed.

        Check your understanding

        A game gives one bonus star for every 50 points. A player has 237 points. Which expression gives the number of whole stars?
        Show the answer

        Answer: A. DIV(237, 50)

        DIV counts how many whole times; MOD gives what is left over.

        Check your understanding

        A program must test whether a whole number, Number, is even. Which condition does it use?
        Show the answer

        Answer: C. MOD(Number, 2) = 0

        A remainder of 0 when dividing by 2 means the number is even.

        Work out each expression. Write the result.

        Working out expressions
        ExpressionResult
        14 + 3 * 2
        (14 + 3) * 2
        2 ^ 4
        DIV(23, 4)
        MOD(23, 4)
        9 / 2

          Show the completed table
          ExpressionResult
          14 + 3 * 220
          (14 + 3) * 234
          2 ^ 416
          DIV(23, 4)5
          MOD(23, 4)3
          9 / 24.5

          Stage: Explain

          Comparing values and combining tests

          In Lesson 2 you used symbols such as <= in a condition. A test like Score > 100 is true or false, and the result is a value of the BOOLEAN data type.

          Think it through

          A theme-park ride is for visitors who are at least 12 and at most 18 years old. A visitor is 14. Is the visitor allowed on? Write the rule using a comparison for each end of the range.

          Show a model answer

          Yes, 14 is allowed. The rule needs two comparisons: Age >= 12 and Age <= 18, and both must be true. They are combined with AND: Age >= 12 AND Age <= 18.

          The relational operators compare two values and give TRUE or FALSE: = (equal to), <> (not equal to), <, <=, > and >=. The logical operators combine TRUE and FALSE values: AND is TRUE only when both sides are TRUE; OR is TRUE when at least one side is TRUE; NOT turns TRUE into FALSE and FALSE into TRUE. They work exactly like the AND, OR and NOT gates, with TRUE and FALSE in place of 1 and 0. Put brackets round the parts of a long condition to make the order clear.

          Each side of AND or OR must be a complete test of its own. The two range tests are written Age >= 12 AND Age <= 18. Writing Age >= 12 AND <= 18 (leaving out the second Age) or 12 <= Age <= 18 is not valid pseudocode.

          Apply it

          Sam writes the condition for the ride as: Age >= 12 AND <= 18. What is wrong with it?
          Show the answer

          Answer: B. The right-hand side has no value to compare: it should be Age >= 12 AND Age <= 18.

          Repeat the variable on each side of AND or OR.

          A visitor has Age 14 and HasTicket TRUE. Work out each condition. Write TRUE or FALSE.

          Age is 14 and HasTicket is TRUE
          ConditionTRUE or FALSE?
          Age >= 12
          Age >= 12 AND Age <= 18
          Age < 12 OR Age > 65
          NOT HasTicket
          Age <> 14
          HasTicket AND Age > 16

            Show the completed table
            ConditionTRUE or FALSE?
            Age >= 12TRUE
            Age >= 12 AND Age <= 18TRUE
            Age < 12 OR Age > 65FALSE
            NOT HasTicketFALSE
            Age <> 14FALSE
            HasTicket AND Age > 16FALSE

            Stage: Apply

            A complete program

            A school canteen till (fictional) sells snacks at 85 pence each. It takes the number of snacks and shows the total in pounds and pence, using whole numbers of pence to avoid fractions.

            CONSTANT SnackPrice ← 85
            DECLARE Quantity : INTEGER
            DECLARE Total : INTEGER
            DECLARE Pounds : INTEGER
            DECLARE Pence : INTEGER
            INPUT Quantity
            Total ← Quantity * SnackPrice
            Pounds ← DIV(Total, 100)
            Pence ← MOD(Total, 100)
            OUTPUT Pounds, " pounds and ", Pence, " pence"

            Apply it

            A pupil buys 4 snacks. What does the program output?
            Show the answer

            Answer: D. 3 pounds and 40 pence

            DIV gives the whole pounds and MOD gives the pence left over.

            Think it through

            Write a program in Cambridge pseudocode that declares what it needs, inputs two test marks (whole numbers out of 20) and outputs their average. Mention which data type the average needs, and why.

            Show a model answer
            DECLARE MarkOne : INTEGER
            DECLARE MarkTwo : INTEGER
            DECLARE Average : REAL
            INPUT MarkOne
            INPUT MarkTwo
            Average ← (MarkOne + MarkTwo) / 2
            OUTPUT Average

            Average is a REAL because dividing by 2 can give a fractional part: marks of 15 and 18 give 16.5. The brackets make the addition happen before the division; without them only MarkTwo would be divided by 2.

            Go deeper The same ideas in Python

            Python is a real programming language, so a computer can run these. The ideas are the same; the notation differs.

            SNACK_PRICE = 85
            quantity = int(input())
            total = quantity * SNACK_PRICE
            pounds = total // 100
            pence = total % 100
            print(pounds, "pounds and", pence, "pence")
            • Python has no DECLARE: a variable exists from its first assignment, and its type comes from the value stored.
            • = stores a value (pseudocode uses ←). == tests for equal, != for not equal; and, or and not are in lower case.
            • // is DIV and % is MOD. ** raises to a power (2 ** 4 is 16), where pseudocode uses ^. Python’s / always gives a real number: 9 / 2 is 4.5 and 8 / 2 is 4.0.
            • input() always gives a string, so int(...) converts it, or float(...) for a real number. Forgetting this is a common error: adding a string and a number stops the program.
            • Python has no true constant. By convention, a name in capital letters such as SNACK_PRICE is not changed.
            • Python has no separate CHAR: a single character is simply a string of length 1, and either 'x' or "x" works.
            • print is Python’s OUTPUT; commas in print put a space between the values, but pseudocode’s OUTPUT does not.

            Stage: Consolidate

            Show what you know

            Think it through

            Explain to a friend the difference between a variable and a constant, with one example of each from a school canteen till.

            Show a model answer

            A variable holds a value that can change while the program runs, such as the running total of a customer’s order. A constant holds a value that is set once and does not change, such as the price of a snack, which is named once (CONSTANT SnackPrice ← 85) so it can be updated in one place.

            Apply it

            A school’s fictional app stores whether each pupil has handed in their homework, and shows ‘Done’ or ‘Missing’. Which data type is best for the variable that records whether the homework is in?
            Show the answer

            Answer: A. BOOLEAN, because it has only two possible values, TRUE or FALSE.

            When a value has only two possible answers, BOOLEAN is the safest type.

            • A variable is a named place that holds a value that can change; an assignment Score ← Score + 10 works out the right side first and stores the result on the left. A constant is set once and not changed.
            • DECLARE Name : TYPE says a variable exists. Names start with a capital letter and use capitals for new words.
            • A data type is the kind of value: INTEGER (whole numbers), REAL (numbers with a fractional part), CHAR (one character), STRING (text) and BOOLEAN (TRUE or FALSE).
            • INPUT stores what the user types; OUTPUT shows values. Instructions run in sequence, so their order matters.
            • Operators: + - * / ^, DIV (whole number of times) and MOD (remainder); the relational operators = <> < <= > >= give TRUE or FALSE; AND, OR and NOT combine them, and each side of AND and OR must be a complete test.
            • Python writes the same ideas differently: // for DIV, % for MOD, == for “equal to”, and no DECLARE.

            Sources and scope

            • What this lesson is. The third lesson of the Algorithms and Programming module, written for this site. It covers points 8.1.1, 8.1.2, 8.1.3, 8.1.4(a) and 8.1.4(f) of Cambridge IGCSE Computer Science 0478 (2026 to 2028, version 6). Its curriculum links are provisional and imply no review or endorsement by any examination board.
            • Original material. Meteor Dash, Maya, Zara, the theme-park ride, the canteen till, the shop program and Sam are fictional, written for this lesson. The pseudocode follows the layout in the syllabus’s pseudocode guide, and each program’s output was worked out by hand.
            • Simplified on purpose. Money is held in whole pence to keep the arithmetic in whole numbers. The rules for the order of the logical operators are not taught; brackets are used instead, as the syllabus advises. A real program would also check that the user types a number where one is needed; that is left to a later lesson on validation.
            • Left to later lessons. Selection (Lesson 4), iteration (Lesson 5), totalling and counting (Lesson 6). String handling, arrays, subroutines and library routines such as ROUND and RANDOM are not covered in this lesson.