HP 3000 Manuals

FOR [ HP Business BASIC/XL Reference Manual ] MPE/iX 5.0 Documentation


HP Business BASIC/XL Reference Manual

FOR 

The FOR and NEXT statements define a loop that is repeated until the
value of the loop control variable is greater than or less than a
specified value.  The value of the loop control variable can increase or
decrease.

Syntax 

     FOR num_var=num_expr1 TO num_expr2 [STEP num_expr3]  [stmt]...

     NEXT num_var 

Parameters 

num_var          The numeric loop control variable that assumes the
                 values num_expr1, num_expr1+num_expr3,
                 num_expr1+(2*num_expr3), etc.  on successive executions
                 of the loop body.  The loop body executes once for each
                 value that is less than or equal to num_expr2 (if
                 num_expr3 is positive) or greater than or equal to
                 num_expr2 (if num_expr3 is negative).

                 The num_var in the FOR and NEXT statements must be the
                 same.

num_expr1        The initial value that num_var is assigned.

num_expr2        Value that num_var is compared to before the loop body
                 is executed.  If num_expr3 is positive and num_var >
                 num_expr2, the loop body is not executed.  Similarly, if
                 num_expr3 is negative and num_var < num_expr2, the loop
                 body is not executed.

num_expr3        Amount that num_var is incremented by (if num_expr2 is
                 positive) or decremented (if num_expr2 is negative)
                 following execution of the statements in the loop body.

stmt             If num_expr2 is positive, this statement or statements
                 executes each time num_var <= num_expr2.  If num_var is
                 negative, this statement executes each time num_var >=
                 num_expr2.  These statements constitute the loop body.

Examples 

     10  FOR I=2 TO 10 STEP 2      !This will loop 5 times.
     20    PRINT "I=",I            !Values are: 2,4,6,8,10
     30    PRINT "I+I=",I+I        !Values are: 4,8,12,16,20
     40    PRINT "I*I=",I*I        !Values are: 4,16,36,64,100
     50  NEXT I

     10  FOR J=4 TO 1 STEP -1      !This will loop 4 times
     20    PRINT J                 !Values 4,3,2,1
     30  NEXT J

     100 FOR K=1 TO 20             !This will loop 20 times
     110   PRINT K                 !Values 1,2,3,...,19,20
     120 NEXT K

If num_expr3 is positive, and the first value is greater than the last
value, or if num_expr3 is negative, and the first value is less than the
last value, then the loop is never executed.

     20 FOR I=10 TO 1
     30   PRINT I      !Never executed
     40 NEXT I

FOR constructs can be nested.

     100 FOR I=1 TO 3          !Outside FOR loop
     110   FOR J=1 TO 5        !Inside FOR loop
     120     PRINT "*";
     130   NEXT J
     135   PRINT
     140 NEXT I
     999 END

The above program prints:

     *****
     *****
     *****


NOTE An error occurs if nested loops have the same loop control variable (num_var). If you use mixed mode arithmetic on the control variable, the results can be unpredictable. For example, if I is an integer, and the STEP value is .1, STEP is rounded to zero.
Entering from a statement other than the FOR statement the body of a FOR loop causes an error at the NEXT statement, because no FOR statement is active.


MPE/iX 5.0 Documentation