HP 3000 Manuals

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


HP Business BASIC/XL Reference Manual

LOOP 

The LOOP, EXIT IF, and ENDLOOP statements define a loop that repeats
until the numeric expression in the EXIT IF statement is TRUE (nonzero).

Syntax 

              [EXIT IF num_expr]
              [[stmt]          ]   {ENDLOOP }
LOOP [stmt]...[.               ]...{END LOOP}
              [.               ]
              [.               ]

Parameters 

stmt             A program line that can be another LOOP statement.
                 These statements constitute the loop body.

num_expr         A numeric expression that determines program control.
                 Considered FALSE if the value following evaluation is
                 zero, TRUE if it evaluates to nonzero.  If FALSE,
                 control is transferred to the line following the EXIT IF
                 statement; if TRUE, control is transferred to the line
                 following the ENDLOOP statement.  If the loop does not
                 contain an EXIT IF statement, and control is not
                 transferred out of the loop by some other means (for
                 example, a GOTO statement) the loop never ends.

Examples 

     100 LET I=0                  !Initialize I
     110 LOOP                     !Begin loop
     120   PRINT I                !Print I (at this line, I=0,1,2,...,99)
     130   LET I=I+1              !Increment I (at this line, I=1,2,3,...,100)
     140   EXIT IF I=100          !If I=100, go to line 160; else go to line 120
     150 ENDLOOP                  !End loop
     160 PRINT I                  !Print I (at this line, I=100)
     999 END

     100 READ I                   !Read number to be guessed, I
     101 Low=1                    !Lowest possible guess
     102 High=100                 !Highest possible guess
     103 Tries=0                  !Number of tries to guess I
     110 LOOP
     111   Tries=Tries+1          !Count one for guessing I=Low in 120
     120   EXIT IF I=Low          !If Low=I, go to 230; else go to 121
     121   Tries=Tries+1          !Count one for guessing I=High in 130
     130   EXIT IF I=High         !If High=I, go to 230; else go to 140
     140   Guess=(Low+High)/2     !Guess average of Low and High
     145   Tries=Tries+1          !Count one for guessing I=Guess in 150
     150   EXIT IF I=Guess        !If Guess=I, go to 230; else go to 160
     160   SELECT Guess-I         !If I<>Guess, reset Low or High
     170   CASE < 0               !If Guess < I,
     180     Low=Guess            !then Guess is the new lowest guess.
     190   CASE > 0               !If Guess > I,
     200     High=Guess           !then Guess is the new highest guess.
     210   END SELECT
     220 END LOOP
     230 PRINT Tries              !Print number of tries needed
     250 DATA 47
     999 END

Loops can be nested.  An EXIT IF statement in a nested loop belongs to
the innermost loop that contains it.

      1 Num_row=4
      2 Num_col=5
     10 Row=1
     11 LOOP                           !Begin outer loop
     12   Column=1
     13   LOOP                         !Begin inner loop
     14     PRINT A(Row,Column)
     15     Column=Column+1
     16     EXIT IF Column=(Num_col+1) !Exit inner loop (go to line 18)
     17   ENDLOOP                      !End inner loop
     18   PRINT
     19   Row=Row+1
     20   EXIT IF Row=(Num_row+1)      !Exit outer loop (go to line 99)
     21 ENDLOOP                        !End outer loop
     99 END

Entering a LOOP construct from a statement other than the LOOP statement
is considered to be a bad programming practice, and is not recommended.
However, calling a local subroutine using GOSUB or calling an external
subroutine using CALL from within a loop construct can be very useful.

     100 I=0
     110 LOOP             !Begin loop
     130   EXIT IF I=100
     140   GOSUB 200      !Leave loop for subroutine
     145   I=I+1          !Reenter loop here
     150 END LOOP         !End loop
     160 STOP
     200 REM Subroutine   !Begin subroutine
     210   PRINT I
     220 RETURN           !Return to loop
     999 END

If a program unit contains an EXIT IF statement that is not in a loop, an
error occurs.



MPE/iX 5.0 Documentation