Data types, variables and control structures

IGCSE Computer Science · Programming

The five data types

integera whole number, positive or negative — $7$, $-42$
reala number with a decimal part — $3.75$, $-0.5$
chara single character — $\text{'A'}$, $\text{'?'}$
stringa sequence of characters — $\text{"Hello"}$
Booleanonly 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

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:

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-controlledFOR … TO … NEXT — used when the number of repetitions is known in advance
pre-conditionWHILE … DO … ENDWHILE — condition tested before the loop body, so it may run zero times
post-conditionREPEAT … 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.

Is the condition checked BEFORE or AFTER the loop body? WHILE — checks BEFORE con- dition? correct loop body sai → thoát MAY RUN 0 TIMES dùng khi danh sách MAY BE EMPTY REPEAT — checks AFTER loop body con- dition? not yet met met ALWAYS RUNS AT LEAST ONCE use when you MUST ask first before there is anything to check
Choosing the wrong type is a logic error — the program still runs and reports nothing, it just processes an element that does not exist.

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.

Common mistake

Using a post-condition loop where a pre-condition check is needed. The two types look very similar when written out, and with ordinary data they give the same result — so this error often slips through every test. The difference only shows up in one single case: when the loop should not have run at all. A pre-condition loop examines the condition before the body, so for an empty list it skips everything. A post-condition loop examines the condition after the body, so it always runs at least once — and on an empty list, it tries to read an element that does not exist. The consequence: either an out-of-range access error, or worse, adding a garbage value to the result without reporting anything. A one-sentence test: ask “is there any case where it needs to run zero times?” Yes → pre-condition. Must be done at least once → post-condition.

Key takeaway

Five data types: integer, real, char, string, Boolean — and telephone numbers are stored as strings because a numeric type throws away the leading 0. A variable can change while the program runs, a constant cannot. DIV gives the whole-number part, MOD gives the remainder; $n \ \text{MOD} \ 2 = 0$ is the standard way to test for an even number. Three structures: sequence, selection, iteration. A count-controlled loop when the number of iterations is known in advance; pre-condition when it may have to run zero times; post-condition when it must be done at least once.

← Testing: validation, verification and test data · All IGCSE Computer Science lessons · Practice IGCSE Computer Science questions (free, instant scoring) · Arrays, subroutines and files →