Skip to content
IGCSE·Tuition
Computer Science · Lessons

Explain the purpose of a variable

You know what the variable does in the code, but the sentence you write keeps coming out as its name again.

On this page
  1. What makes a purpose answer complete?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

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.

  1. Holds: the thing the variable stores at any moment, such as “the running total of marks”.
  2. Changes: when and how it is updated, such as “increased each time a mark of 50 or more is read”.
  3. 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.

Questions people ask

What should a purpose answer include?

Say what value the variable holds, when that value changes, and why the algorithm needs it. For a counter, add what is being counted. For a total, add what is being added up. A short answer with these three parts usually covers what the question asks.

Why does a variable start at 0 or another value?

The starting value must make the first update correct. A total starts at 0 because adding to 0 leaves the first number unchanged. A counter starts at 0 because nothing has been counted yet. Say this reason in your answer, not just the value.

Is it enough to write 'Count counts'?

No. That repeats the name and adds no information. Say what is counted, for example 'Count holds how many entered ages are 18 or over so far'. The marker needs to see that you understand the role in this algorithm.

Sources

  1. Cambridge IGCSE Computer Science 0478 syllabus page

Updated:

Your next step

If your explanations sound right in your head but read vaguely on paper, a one-to-one teacher can mark your sentences with you and show what a full answer contains.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service