UNIT 1 - ⇑ Fundamentals of Programming ⇑

← Global and Local Variables The Role of Variables Fundamentals of Structured Programming →


As we have seen variables can play a very important role in creating programs and especially when executing loops. The roles of these variables can be categorised into several types:

Type Description Examples
fixed value A variable that is given a value that then does not change for a duration of a loop This might be an upper or lower bounds that the array is tending towards
stepper A variable used to move through an array or other data structure, often heading towards a fixed value and stepping through elements in an array Stepping through the values in an array, or outputting a certain number of lines of text
most recent holder A variable used to record the last thing inserted by a user or the latest value being read from an array input = console.readline() or getting the next value from an array
gatherer A variable that accumulates or tallies up set of data and inputs. It is very useful for calculating totals or totals that will be used to calculate averages. the total of ages of students in a class, the scores or batsmen in a cricket game, tally UMS points for an A-Level student or tell you the combined price of all the objects in your shopping basket
Example: fixed value / stepper / most recent holder
dim fixedvalue as integer = 10
for stepper = 1 to fixedvalue
  console.writeline("the stepper value is : " & stepper & " and the fixed value is : " & fixedvalue)
next

in the above code it is easy to see the use of a fixed value and a stepper. Let's look at a more complicated example:

dim totalnum as integer = 10
dim t as integer = 0
dim input as integer
console.writeline("The following loop will collect 10 values and gather them together:")
for x = 1 to totalnum
  console.writeline("the stepper value is : " & x & " the fixed value is : " & totalnum )
  console.writeline("please insert a value:")
  input = console.readline()
  t = t + input 
  console.writeline("so far we have gathered numbers totalling:" & t)
next

In the above code you can see the various roles of variables in collecting together 10 inputs and adding them all together (gathering):

Variable Role
totalnum fixed value
x stepper
t gatherer
input most recent holder
Type Description Examples
most wanted holder A variable that keeps track of the lowest or highest value in a set of inputs calculating the top scorer in a football team, the lowest score in an exam and the highest number of pied wag tails seen by a bird watcher in a particular year
follower used to keep check of a previous value of a variable, so that a new value can be compared followers are often used when sorting arrays, in routines such as bubble or insertion sort
temporary A variable used for storing something for a very short period of time temporary variables are often used for swapping values between other variables
transformation used to store the result of a calculation involving more than one variable transformation variables are used to calculate such things as compound interest
Example:most wanted holder / transformation / follower / temporary
dim scores() as integer = {12,32,43,2,11,23,7,9}
dim mostwantedholder as integer = 0
for c = 0 to 7
  if scores(c) > mostwantedholder then
    mostwantedholder = scores(c)
  end if 
next
console.writeline("the highest score is: " & mostwantedholder)

In the code above you can see the use of the most wanted holder to store the maximum value from an array of numbers. As you cycle through each item in the array (using the stepper c), you update the mostwantedholder to store the maximum value that you come across. Let's take a look at a more complex example

dim scores() as integer = {12,32,43,2,11,23,7,9}
dim temp, prev as integer
dim arLen as integer = 7
dim mostwantedholder as integer = 0

for c = 1 to arLen
  prev = scores(c-1)
  if prev > scores(c) then
    temp = prev
    prev = scores(c)
    scores(c) = temp
  end if
next

The code above describes a single pass of bubble sort. Using the temporary variable, temp, we bubble the largest array value to the top of the array, by comparing the current array value (scores(c)) and the follower prev. In summary:

Variable Role
temp temporary
prev follower
arLen fixed value
c stepper

Finally let's look at an example of a transformation, we have used lots of loops so far, but variables certainly aren't only used in loops:

Const pi as single = 3.14
dim r as single
dim a, c as single
console.write("insert the radius: ")
r = console.readline()
a = pi * r * r
console.writeline("area = " & a)
c = 2 * pi * r
console.writeline("circumference = " & c)

In the code above there are two transform variables a and c.

Exercise: Role of Variables
dim numCats as integer = 9
for x = 1 to numCats
  console.writeline("cat number :" & x & " says meow!")
next

For the above code name the role of each of the following variables:

Variable Role
x
numCats

Answer:

Variable Role
x stepper
numCats fixed value
dim total, highest, avg, current as integer = 0
dim max as integer = 10
console.writeline("insert " & max  & " numbers:")
for count = 1 to max
  current = console.readline()
  total = total + current
  avg = total / count
  if current > highest then
    highest = current
  end if
next

For the above code name the variables that act as a:

Role Variable
transformation
most wanted holder
most recent holder
gatherer
fixed value
stepper

Answer:

Role Variable
transformation avg
most wanted holder highest
most recent holder current
gatherer total
fixed value max
stepper count
Extension: Other Roles

The roles you see here are included in the syllabus and it is very likely that they will be examined. There are several other roles out there which aren't covered here, these include:

  • one-way flag
  • organizer
  • container
  • walker

You can find out more about them here