Presentation is loading. Please wait.

Presentation is loading. Please wait.

Role of variables. Fixed value Description: A variable without any calculation and not changed thereafter; for example we may need to remember the number.

Similar presentations


Presentation on theme: "Role of variables. Fixed value Description: A variable without any calculation and not changed thereafter; for example we may need to remember the number."— Presentation transcript:

1 Role of variables

2 Fixed value Description: A variable without any calculation and not changed thereafter; for example we may need to remember the number of array elements in use. Code: ThisElement = 1 Found = False Repeat If List[ListElement] = ElementSought Then Found = True Else ThisElement = ThisElement+1 Until (Found = True) or (ThisElement > Max)

3 Stepper Description: A variable stepping through a systematic, predictable succession of values; for example, during iterations, a variable is used to keep a count of the number of repetitions. Code: Var Count: Integer; For Count := 1 to 5 Do Writeln(‘Line ’, Count);

4 Most recent holder Description: A variable holding the latest value encountered when processing a succession of unpredictable values or simply the latest value obtained as input; for example, storing the latest of a series of values input by the user to add to a running total. Code: Repeat Writeln(‘Enter a name, X to finish: ‘); Readln(Name); Until Name = ‘X’;

5 Most wanted holder Description: A variable holding the most appropriate value encountered so far; for example, when we search through a set of values for the largest value, we store the largest value we have encountered so far. Code: ThisElement = 1 Found = False Repeat If List[ListElement] = ElementSought Then Found = True Else ThisElement = ThisElement+1 Until (Found = True) or (ThisElement > Max)

6 Gatherer Description: A variable accumulating the effect of individual values; for example, when we calculate the total of a series of values, we keep a running total of all the values added so far. Code: Program RejectReport; Var DayNo: Integer; RejectTotal: Integer; DailyRejects: Array[1..7] of Integer; Begin RejectTotal := 0; For DayNo := 1 to 7 Do RejectionTotal := RejectTotal – DailyRejects[DayNo]; Writeln[RejectTotal]; End.

7 Transformation Description: A variable that always get its new value from a fixed calculation of values of other variables; for example, we store the result of a conversion of a measurement in inches to a measurement in metres. Code: Var Inches, Meters: integer; Begin Readln(Inches) Meters:=inches*0.0254 Writeln(Meters); Readln; End;

8 Follower Description: A variable that gets its new value from the old value of some other data; for example, before updating a variable its current value is copied to the follower. Code: Var I,j: integer; Begin i:= 10; j:=15; i:=j; j:=0; End;

9 Temporary Description: A variable holding some value for a short time only; for example, it is used as the intermediate storage when swapping values in variables. Code: Begin Temp := a; a := b; b := Temp; End;


Download ppt "Role of variables. Fixed value Description: A variable without any calculation and not changed thereafter; for example we may need to remember the number."

Similar presentations


Ads by Google