Repeated Evaluation with the For Command

The for command allows you to execute a command a specified number of times. This provides another way of listing each element of a series or array. You can also use the index of the for command in the expression of the for statement. Consider the series UnitSales and the index variable I, defined in the program as

series UnitSales =
   from 1: 36075,
   from 2 to 5: UnitSales[$ - 1] * 1.15;
value I = 0;

To use I as an index of the for command, it must be declared as a value in the program. Here, I is arbitrarily set to 0. The actual number is not important because I is reset in the for statement. For example, the following command lists the five elements of UnitSales, indexed by I:

for(I = 1 to 5) UnitSales[I]

The effect of executing this command is the same as if the following sequence of commands had been executed:

UnitSales[1];
UnitSales[2];
UnitSales[3];
UnitSales[4];
UnitSales[5];

In either case, the Session Log displays the following output:

= 36075
= 41486.3
= 47709.2
= 54865.6
= 63095.4

To write a row or column of an array, you must specify both subscripts.

for(I = 0 to 3) Numbers[1][I]

This command tells the Log to display the elements of the second row (row 1) of Numbers.

= 5
= 6
= 7
= 8

You can also use the display function in the expression evaluated by the for command.

for(I = 0 to 2) display(Numbers[I][I],"%lg\n")

The Session Log displays the elements Numbers[0][0], Numbers[1][1], and Numbers[2][2], each on a separate line.

1
6
11

As your command procedure gets more complex, you will find the display function very useful. With the display function, we could format the forecast output to look more like a report.

display("Year ");
for(I = 0 to 4) display(1986 + I," -%lg- ");
display("\n");
display("UnitSales");
for(I = 1 to 5) display(UnitSales[I],"%9.5lg ");
display("\n");

The Session Log displays the information in a nice report format. In this case, the field width is specified as 9 characters long and the number of significant digits is set

to 5.

Year        -1986-  -1987-  -1988-  -1989-  -1990-
UnitSales    36075   41486   47709   54866   63095 

Versions: DPL Professional, DPL Enterprise, DPL Portfolio

See Also

Working with Command Statements