HP 3000 Manuals

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


HP Business BASIC/XL Reference Manual

ON GOSUB 

The ON GOSUB statement is one of the GOSUB corollaries of the ON GOTO and
GOTO OF statements.  Control is transferred to the selected line, L, by
"GOSUB L" rather than "GOTO L." A RETURN statement returns control to the
statement that follows the ON GOSUB statement.  Although the ON GOSUB
statement can be input as ON GOSUB or ON GO SUB, HP Business BASIC/XL
will always list it as ON GOSUB.

Syntax 

            {GOSUB }
ON num_expr {GO SUB} line_id [, line_id]...[ELSE else_line_id]

Parameters 

num_expr         A numeric expression that is evaluated and converted to
                 an integer, n.  The integer n must be between one and
                 the number of line_ids, or an error occurs if no ELSE
                 clause is present.  Control is transferred to the nth
                 line_id.

line_id          Line number or line label of the line to which control
                 is transferred.  The line must be in the same program
                 unit as the ON GOSUB statement.

else_line_id     Line number or line label of the line to which control
                 is transferred if the value of num_expr is not between
                 one and the number of line_ids specified.

Examples 

      1 READ I, J
      2 ON I GOSUB 10,20,30         !Go to subroutine at line 10, 20, or 30
      3 ON J GOSUB 40,50,60 ELSE 99 !Go to subroutine at line 40,50, or 60 or to
                                    !line 99
      4 STOP                        !if J < 1 or J > 3
     10 REM Subroutine for I=1
     11   PRINT "I is one"
     12   RETURN                    !Return to line 3
     20 REM Subroutine for I=2
     21   PRINT "I is two"
     22   RETURN                    !Return to line 3
     30 REM Subroutine for I=3
     31   PRINT "I is three"
     32   RETURN                    !Return to line 3
     40 REM Subroutine for J=1
     41   PRINT "J is one"
     42   RETURN                    !Return to line 4
     50 REM Subroutine for J=2
     51   PRINT "J is two"
     52   RETURN                    !Return to line 4
     60 REM Subroutine for J=3
     61   PRINT "J is three"
     62   RETURN                    !Return to line 4
     90   DATA 3,2
     99 END

The GOSUB OF statement works exactly the same as the ON GOSUB statement.
The following statements are equivalent:

     150 ON I GOSUB 10, 20, 30, Quit
     150 GOSUB I OF 10, 20, 30, Quit



MPE/iX 5.0 Documentation