To explain the purpose of a variable, say what value it holds, when it changes and why the algorithm needs it. Questions with “state the purpose of the variable” or “explain why X is used” appear whenever a short algorithm is given and you must show you have read it.
This lesson opens the problem-solving explanations module. It builds on the way variables are assigned and traced in pseudocode for the applicable syllabus.
What makes a purpose answer complete?
A good answer has three parts.
- Holds: the thing the variable stores at any moment, such as “the running total of marks”.
- Changes: when and how it is updated, such as “increased each time a mark of 50 or more is read”.
- Why: the job it does for the algorithm, such as “so the final count can be output after the loop”.
The common roles are a counter, a running total, a largest or smallest so far, a flag (TRUE or FALSE), a loop index and a temporary store. Naming the role helps, but the sentence must still say what is counted or stored.
Worked example
Total ← 0
FOR Index ← 1 TO 4
INPUT Mark
IF Mark >= 50 THEN
Total ← Total + 1
ENDIF
NEXT Index
OUTPUT Total
The marks entered are 62, 45, 50 and 38. Explain the purpose of Total, Mark and Index.
Total. It is a counter. It holds how many of the marks entered so far are 50 or more. It starts at 0 because no marks have been counted yet, and goes up by 1 only when the IF condition is true.
Mark. It holds the one mark that has just been entered. It is overwritten by each new INPUT, so only the current mark is tested.
Index. It is the loop counter. It holds which of the four marks is being processed, 1 to 4, and makes the loop repeat exactly four times.
Check with the data. 62 is counted (Total 1). 45 is not. 50 is counted because the test is >= (Total 2). 38 is not. The output is 2, which matches “how many marks are 50 or more”.
The mistake to watch for
A weak answer repeats the name or the code:
Total stores the total. or Total is Total + 1.
This gives no information the question did not already have. It does not say what is being totalled, and here it is not even a sum of marks, it is a count.
The correction: “Total is a counter that holds how many of the entered marks are 50 or more. It starts at 0 and increases by 1 each time a mark passes the test, so it can be output at the end.”
Check yourself
1. Explain the purpose of Largest in this code.
Largest ← 0
FOR I ← 1 TO 3
INPUT N
IF N > Largest THEN
Largest ← N
ENDIF
NEXT I
OUTPUT Largest
Show answer
Largest holds the biggest number entered so far. It is replaced by N each time a bigger number is read. It starts at 0 so the first positive number replaces it, and at the end it is output as the answer. (It would give a wrong answer if every number entered were negative, since 0 would stay the largest.)
2. In the swap below, explain why Temp is needed.
Temp ← A
A ← B
B ← Temp
Show answer
Temp holds the original value of A for a short time. A is overwritten with B on line 2, so without Temp the old value of A would be lost and B could not receive it. For example, if A = 3 and B = 8, then Temp = 3, A = 8, B = 3.
3. A student writes: “Found is used to find.” Rewrite it for a search that sets Found to FALSE at the start and to TRUE when the target is seen.
Show answer
Found is a flag. It holds TRUE if the target value has been seen in the list and FALSE if not. It starts as FALSE because nothing has been found yet, becomes TRUE when a match occurs, and is tested after the loop to decide which message to output.
Where this leads next
Next, move from explaining a variable to defending a test in justify a test case with a requirement. The restricted pseudocode trace trainer shows how each variable changes, which helps you describe its role.
If you understand the code but your written sentences lose marks, our teachers can read them with you in online one-to-one Computer Science tuition.