Variables, data types and operators

Computer Science · Programming. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science topics

The idea

A program stores data in named memory locations. A variable holds a value that can change while the program runs. A constant holds a value that is set once and stays fixed. Each one has an identifier (its name) and a data type that says what kind of value it holds.

Choose the data type from how the data is used. Numbers used in calculations are INTEGER or REAL. Anything that is not calculated with, such as a phone number, is a STRING.

Data comes in with INPUT and goes out with OUTPUT. Operators then work on the values: arithmetic operators calculate, relational operators compare, and logical operators join conditions.

In simpler words

A program keeps data in named boxes in memory. A variable is a box whose value can change. A constant is a box whose value stays fixed. Each box has a name and a data type, such as a whole number or text. INPUT puts data in. OUTPUT shows it. Operators do sums, make comparisons and join conditions.

An everyday comparison

A variable is like a labelled jar in a kitchen. The label is the identifier, and the jar can be emptied and refilled with something new. A constant is like a jar with its lid glued on. The comparison stops working because a variable's data type is fixed, so a jar labelled INTEGER can only ever hold whole numbers, not text.

Rules to remember

Common mistake

Many students work from left to right and forget that * and / are done before + and −. They also mix up DIV (the whole-number quotient) and MOD (the remainder).

In the exam

When asked for a declaration, write it in full, for example DECLARE Score : INTEGER, and use ← for assignment. Choose the data type by how the data is used, and give phone numbers as STRING with a reason. For an expression, apply the order of operations step by step. Show DIV and MOD results separately, such as DIV(17, 5) = 3 and MOD(17, 5) = 2.

Worked example

What is the value of X after X ← 3 + MOD(20, 6) * 2 ^ 2 ?

  1. MOD(20, 6): 20 ÷ 6 = 3 remainder 2, so MOD(20, 6) = 2.
  2. Power next: 2 ^ 2 = 4.
  3. Multiply before adding: 2 * 4 = 8.
  4. Add: 3 + 8 = 11.

Answer: X = 11

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← Trace tables and correcting algorithmsSequence, selection, iteration and string handling →Take a Computer Science test