HP 3000 Manuals

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


HP Business BASIC/XL Reference Manual

WHILE 

The WHILE and END WHILE statements define a loop that repeats until the
numeric expression in the WHILE statement evaluates to FALSE (zero).

Syntax 

     WHILE num_expr [DO] [stmts]...END WHILE

Parameters 

num_expr         Considered FALSE if it evaluates to zero; TRUE
                 otherwise.  If it is TRUE, the statement or statements
                 in the loop are executed.  If it is FALSE, the statement
                 following ENDWHILE is executed.  Following execution of
                 the statements in the loop body, num_expr is again
                 evaluated to determine whether the loop body is executed
                 again.

stmts            Program lines that are executed if num_expr is TRUE.
                 These statement constitute the loop body.

Examples 

     10 I=50       !Let I be the first number to be printed, 50
     20 WHILE I<>0 !If I<>0, execute loop (lines 30 and 40)
     30   PRINT I  !Print current number, I
     40   I=I-1    !Let I be the next number to be printed
     50 END WHILE  !Return to line 20
     99 END

WHILE constructs can be nested.

     100 Num_rows=3            !Number of rows in matrix M
     110 Num_cols=4            !Number of columns in matrix M
     120 Row=1                 !Let Row be first row to be printed
     130 WHILE Num_rows-Row    !BEGIN OUTER WHILE LOOP
     140   Col=1               !Let Col be first column to be printed
     150   WHILE Num_cols-Col  !BEGIN INNER WHILE LOOP
     160     PRINT M(Row,Col)  !Print one matrix element
     165     Col=Col+1         !Let Col be next column to be printed
     170   END WHILE           !END INNER WHILE LOOP
     180   Row=Row+1           !Let Row be next row to be printed
     190 END WHILE             !END OUTER WHILE LOOP
     999 END

Entering a WHILE loop from a statement other than the WHILE statement is
considered to be a bad programming practice, and is not recommended.  Use
of a GOSUB or CALL statement from within a WHILE loop can be useful.

     100 PRINT "Sum of the odd numbers 1 to 150 is: "
     110 N=1
     120 Sum=0
     130 WHILE 150-N                  !Begin loop
     140   IF N MOD 2 THEN CALL Odd(Sum,N)
     150   N=N+1
     160 ENDWHILE                     !End loop
     170 PRINT Sum
     180 END
     190 !
     200 SUB Odd(Sum,N)               !Return to loop
     210   Sum=Sum+N
     220 SUBEND                       !Return to next line following CALL



MPE/iX 5.0 Documentation