Data types, variables and control structures
IGCSE Computer Science · Programming
The five data types
| integer | a whole number, positive or negative — $7$, $-42$ |
| real | a number with a decimal part — $3.75$, $-0.5$ |
| char | a single character — $\text{'A'}$, $\text{'?'}$ |
| string | a sequence of characters — $\text{"Hello"}$ |
| Boolean | only two possible values: TRUE or FALSE |
Choosing the right type matters: storing a price as an integer loses the pence, and storing a phone number as an integer loses a leading zero — which is why phone numbers are stored as strings.
Variables and constants
- a variable holds a value that can change while the program runs;
- a constant holds a value that is fixed when the program is written and cannot change while it runs.
Why use constants: a value like a tax rate appears in many places. Declaring it once as a constant means changing it in one place, and it removes the risk of accidentally overwriting it.
Operators
Arithmetic: $+$, $-$, $*$, $/$, and two that need care:
- DIV — integer division, giving the whole-number part of the quotient: $17 \ \text{DIV} \ 5 = 3$;
- MOD — giving the remainder: $17 \ \text{MOD} \ 5 = 2$.
A standard use of MOD: testing divisibility. If $n \ \text{MOD} \ 2 = 0$ then $n$ is even.
Comparison: $=$, $<>$ (not equal), $<$, $>$, $<=$, $>=$.
Boolean: AND (both must be true), OR (at least one true), NOT (reverses).
The three control structures
Sequence — statements carried out one after another, in order.
Selection — a choice between paths, using IF … THEN … ELSE … ENDIF, or CASE OF when there are many possible values of one variable.
Iteration — repetition, in three forms:
| count-controlled | FOR … TO … NEXT — used when the number of repetitions is known in advance |
| pre-condition | WHILE … DO … ENDWHILE — condition tested before the loop body, so it may run zero times |
| post-condition | REPEAT … UNTIL — condition tested after the loop body, so it always runs at least once |
This distinction decides marks. Consider validating an input: a REPEAT loop is correct, because you must ask at least once before you have anything to check. Consider processing a list that might be empty: a WHILE loop is correct, because it must be able to run zero times.
Choosing the wrong one produces a logic error: with a REPEAT loop on an empty list, the program processes an item that is not there.
Worked example: choosing the right loop type
For each situation, state the correct loop type and explain: (a) printing the $7$ times table from $1$ to $12$; (b) asking the user to enter a password until it is correct; (c) calculating the sum of the numbers in a list that may be empty.
Solution
(a) Printing the $7$ times table → a COUNT-CONTROLLED LOOP.
Reason: we know in advance exactly how many iterations there are — exactly $12$, no more, no fewer.
This is precisely the condition for using $\textbf{FOR} \ i \leftarrow 1 \ \textbf{TO} \ 12$: when the number of iterations is a figure known right from the time of writing the program.
Using WHILE or REPEAT here would still work, but you would have to write the counter increment and the stopping condition yourself — longer and more error-prone.
(b) Entering a password until correct → a POST-CONDITION LOOP.
Reason: you must ask at least once before there is a password to check.
If you use a pre-condition loop, the program would check the condition before the user has typed anything — and at that point the password variable is still empty or undefined.
The correct structure:
$\textbf{REPEAT}$
$\quad \textbf{INPUT } \text{password}$
$\textbf{UNTIL } \text{password} = \text{correct\_password}$
This loop always runs at least once, exactly what we need.
(c) Summing a list that may be empty → a PRE-CONDITION LOOP.
Reason: the list may have no elements at all, so the loop must be able to run zero times.
A pre-condition loop does exactly that: it checks “are there any elements left?” before entering the body, so for an empty list it skips everything and returns a total of $0$ — completely correct.
If REPEAT is chosen by mistake. The program would run the loop body once even though the list is empty, meaning it tries to read an element that does not exist.
The result: either the program reports an out-of-range access error, or worse, it adds a garbage value to the total and gives a wrong result without reporting anything.
A concise principle: ask “is there any case where it needs to run zero times?” Yes → pre-condition. No, it must be done at least once → post-condition.
Worked example: choosing data types and using MOD
(a) State the suitable data type for: a student's age; the price of a book; a telephone number; the answer “yes/no”. (b) Explain $17 \ \text{DIV} \ 5$ and $17 \ \text{MOD} \ 5$, and state one practical use of MOD.
Solution
(a) Choosing data types.
A student's age → integer. Age is counted in whole years so no decimal part is needed.
The price of a book → real. A price has a fractional part. If it were stored as an integer the fractional part would be cut off, and every money calculation afterwards would be wrong.
A telephone number → string. This is the choice most often got wrong, because a telephone number “looks like a number”.
There are two reasons to use a string. First, telephone numbers often have a leading 0, and a numeric type throws away the leading 0 — $\text{0912}$ becomes $912$. Second, we never calculate with telephone numbers: adding two telephone numbers is meaningless.
A yes or no answer → Boolean. It has exactly two values, so the Boolean type is the perfect and most economical fit.
(b) DIV and MOD.
$17 \ \text{DIV} \ 5 = 3$ — integer division gives the whole-number part of the quotient. Dividing $17$ by $5$ gives $3$ complete times.
$17 \ \text{MOD} \ 5 = 2$ — the modulus gives the remainder. After taking out $3 \times 5 = 15$, $2$ is left.
A way to remember the link: $17 = (5 \times 3) + 2$, that is, $\text{dividend} = (\text{divisor} \times \text{DIV}) + \text{MOD}$.
Note the difference from ordinary division: $17 / 5 = 3.4$, a real number. Both DIV and MOD give integers.
A practical use of MOD: testing divisibility.
If $n \ \text{MOD} \ 2 = 0$ then $n$ is an even number; otherwise it is odd. This is the standard way to test for even or odd, because there is no separate operator for it.
The same principle: $n \ \text{MOD} \ 3 = 0$ means $n$ is divisible by $3$.
A second use: separating the digits of a number. With $n = 47$, $n \ \text{MOD} \ 10 = 7$ gives the units digit, and $n \ \text{DIV} \ 10 = 4$ gives the rest.