Previous Page | Next Page

SCL Arrays

Repeating an Action for Variables in an Array in SCL Programs

To perform an action on variables in an array, you can use an iterative DO statement, using the index variable for the array subscript. A DO block is especially convenient when arrays contain many elements. For example, you could use a program like the following to sum the values of the array variables and to display the total in the SUM field:

array month[5] jan feb mar apr may (1,2,3,4,5);
INIT:
   do i=1 to 5;
      sum+month[i];
   end;
   put month;
   put sum=;
return;

The example produces the following output:

month[1] = 1
month[2] = 2
month[3] = 3
month[4] = 4
month[5] = 5
sum=15

The preceding DO block has the same effect as any one of the following assignment statements:

sum1=jan+feb+mar+apr+may;
sum2=sum(of month[*]);
sum3=sum(of jan--may);
put sum1= sum2= sum3= ;

This example produces the following output:

sum1=15 sum2=15 sum3=15

Previous Page | Next Page | Top of Page