HP Business BASIC/XL Reference Manual - HP 3000 MPE/iX Computer Systems - Edition 1 (32715-90001)

4- 118
FOR Loops in READ statements
The READ statements in the previous examples have contained only
references to individual variables. A READ statement can contain a FOR
loop designed to assign values to specific array elements or substrings
of a string variable.
Syntax
(FOR
num_var
=
num_expr1
TO
num_expr2
[STEP
num_expr3
],
input_list
)
Parameters
num_var
The numeric loop control variable that assumes the
values
num_expr1
,
num_expr1
+
num_expr3
,
num_expr1
+(2*
num_expr3
) on successive executions of the
loop body.
num_expr1
The initial value that
num_var
is assigned.
num_expr2
Value to which
num_var
is compared before the loop body
is executed.
num_expr3
Amount by which
num_var
is incremented or decremented.
The default is one.
input_list
The list of items to be read. This is the same as for
the READ statement without the FOR loop.
A READ statement executes the following steps each FOR loop specified:
1.
num_var
=
num_expr1
2. If
num_expr3
is positive; go to step 3 else
num_expr3
is negative
got to step 4.
3. If
num_var
<=
num_expr2
, then assign data to the
input_list
elements, and go to step 5; otherwise, stop.
4. If
num_var
>=
num_expr2
, then assign data to the
input_list
elements, and go to step 5; otherwise, stop.
5.
num_var
=
num_var
+
num_expr3
.
6. Return to step 3 or 4 if
num_expr3
is
positive
or
negative
,
respectively.
Examples
A variable specified within a FOR loop in a READ statement must contain a
reference to
num_expr1
as a subscript or substring if the data are not to
be repeatedly assigned to the same variable or array element. When
100 READ (FOR I=1 TO 4 STEP 1 A(I))
is executed, the index I assumes the values 1, 2, 3, and 4 and assigns
the data to the array elements A(1), A(2), A(3), and A(4). The statement
200 READ (FOR I=2 to 6 STEP 2, A$[I;1])
assigns the first character in each of the next three double-quoted
string literal data items to positions 2, 4 and 6 in A$.
FOR loops within READ statements can be nested; for example, the
following statement reads data into a 3-by-5 array.
250 READ (FOR I=1 TO 3, (FOR J=1 TO 5, A(I,J))