2.5 Role of a Variable Flashcards
Fixed Value
The role of a variable is a fixed value if its value is not changed while the program is running after initialization.
The example program asks user the radius of a circle and then prints out the area of that circle. Variable radius is a fixed value. This variable gets its value (on line 7) during the program’s run once only and the value never changes after that. A fixed value can be used in different places in a program - twice on line 8 in the example.
(1) PROGRAM AreaOfCircle (input,output);
(2) CONST PI = 3.14;
(3) VAR radius: real;
(4) BEGIN
(5) writeln;
(6) write(‘Enter the radius of a circle: ‘);
(7) readln(radius);
(8) writeln(‘The area of the circle is ‘, PI * radius * radius)
(9) END.
Stepper
Stepper goes through a succession of values in some systematic way.
Below is an example of a loop structure where the variable multiplier is used as a stepper. The example program outputs a multiplication table while the stepper goes through the values from one to ten.
(1) PROGRAM MultiplicationTable (output);
(2) VAR multiplier: integer;
(3) BEGIN
(4) FOR multiplier := 1 TO 10 DO
(5) writeln(multiplier, ‘ * 3 = ‘, multiplier3)
(6*) END.
A stepper can be used, for example, for counting and traversing an array.
Most-recent holder
A variable is a most-recent holder if its value is the latest gone through value of a certain group or simply the latest input value.
The example program repeatedly (on line 6) asks the user for the input until the input is valid. In this program the variable side is a most-recent holder since it holds the latest input value at the time.
(1) PROGRAM AreaOfSquare (input,output);
(2) VAR side: real;
(3) BEGIN
(4) REPEAT
(5) write(‘Enter the length of the side of a square: ‘);
(6) readln(side)
(7) UNTIL side > 0;
(8) writeln(‘The area of the square is ‘, side * side)
(9) END.
Most-wanted holder
The value of a most-wanted holder is the best or otherwise the most-wanted value out of the values gone through so far. There are no restrictions for measuring the superiority of the values: the most-wanted can mean, for example, the smallest or the biggest number or a number closest to a certain value.
The example program finds out which is the smallest of the ten integers given as an input. The variable smallest is a most-wanted holder since it is given (on line 8) the most recent value if it is smaller than the smallest one so far.
(1) PROGRAM SearchSmallest (input,output);
(2) VAR i, smallest, number: integer;
(3) BEGIN
(4) write(‘Enter the first number: ‘); readln(smallest);
(5) FOR i := 2 TO 10 DO
(6) BEGIN
(6) write(‘Enter number ‘, i, ‘: ‘);
(7) readln(number);
(8) IF number < smallest THEN smallest := number
(9) END;
(10) writeln (‘The smallest number was ‘, smallest)
(11) END.
The variable i is a stepper and number is a most-recent holder.
Gatherer
The value of a gatherer is to accumulate all the values gone through so far.
The example program accepts integers given as an input one by one until the user inputs the number -999 after which the program calculates the mean value of the inputs. The variable sum is a gatherer: the total of the inputs is gathered (on line 10) in it.
(1) PROGRAM MeanValue (input,output);
(2) VAR count: integer;
(3) sum, number: real;
(4) BEGIN
(5) sum := 0;
(6) count := 0;
(7) REPEAT
(8) write(‘Enter a number, -999 to quit: ‘);
(9) readln(number);
(10) IF number <> -999 THEN sum := sum + number;
(11) IF number <> -999 THEN count := count + 1
(12) UNTIL number = -999;
(13) IF count > 0 THEN writeln(‘The mean value is ‘, sum / count)
(14) END.
The variable count is a stepper and number is a most-recent holder.
Follower
A follower always gets the old value of another known variable as its new value. The example program asks the user for twelve integers and finally outputs the biggest difference between the two successive input integers. The variable previous is a follower: it follows the variable current (on line 9).
(1) PROGRAM BiggestDifference (input,output);
(2) VAR month, current, previous, biggestDifference: integer;
(3) BEGIN
(4) write(‘Enter the first value: ‘); readln(previous);
(5) write(‘Enter the second value: ‘); readln(current);
(6) biggestDifference := current - previous;
(7) FOR month := 3 TO 12 DO
(8) BEGIN
(9) previous := current;
(10) write(‘Enter the value for month: ‘, month);
(11) readln(current);
(12) IF (current – previous) > biggestDifference THEN
(13) biggestDifference := current - previous
(14) END;
(15) writeln(‘The biggest difference was ‘, biggestDifference)
(16) END.
The variable month is a stepper, current is a most-recent holder and biggestDifference is a most-wanted holder.
Followers are used a lot with linked data structures to point to the data element previous to the one in process.
Transformation
A variable is a transformation variable if it always gets its new value from a fixed calculation of values of other variables. For example, a program might store the result of a conversion of a measurement in inches to a measurement in centimetres by multiplying the inches value by 2.54.
In the sample program for the temporary role in the next section tax is a transformation variable.
Temporary
A variable is a temporary if its value is always needed only for a very short period.
The example program asks the user for tax-free prices and prints out the total price and the tax for each item. The tax is computed into the temporary tax (on line 9) which is immediately used twice in the next statement (on lines 10 and 11).
(1) PROGRAM Taxation(input,output);
(2) CONST taxRate = 16;
(3) VAR taxFree, tax: real;
(4) BEGIN
(5) REPEAT
(6) write(‘Enter tax-free price (0 to quit): ‘); readln(taxFree);
(7) IF taxfree <> 0 THEN
(8) BEGIN
(9) tax := taxFree * taxRate / 100;
(10) writeln(‘Total price ‘, taxFree + tax : 1 : 2,
(11) ‘ that includes tax ‘, tax : 1 : 2)
(12) END
(13) UNTIL taxFree = 0
(14) END.
The variable taxFree is a most-recent holder, tax is a transformation variable.
A temporary is typically used for efficiency reasons (e.g., executing a calculation, whose value is needed in two places, only once) or to clarify the program (e.g., storing a result even though the use of a variable would not be absolutely necessary).