Q(uick)BASIC Statement: ON...GOSUB
Quick View
ON...GOSUB
A control flow statement that branches to one of several specified lines, depending on the value of an expression
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- ON expression% GOSUB line-list
- ON expression% GOTO line-list
Description/Parameter(s)
expression% | An expression in the range 0 through 255. |
line-list | A set of labels or line numbers. If the value of the expression is 1, the program branches to the first line in the list; if the expression is 2, it branches to the second line, and so on. |
SELECT CASE provides a better way to perform multiple branching. |
Example
FOR i% = 1 TO 2
ON i% GOSUB One, Two
NEXT i%
END
One: PRINT "One"
RETURN
Two: PRINT "Two"
RETURN
See also:
Syntax
- ON expression GOSUB {line-number-list | line-label-list}
- ON expression GOTO {line-number-list | line-label-list}
Description/Parameter(s)
The expression argument can be any numeric expression (expression is rounded to an integer before the ON...GOSUB or ON...GOTO is evaluated). The line-number-list or line-label-list consists of a list of line numbers or line labels, separated by commas. The value of expression determines which line the program branches to. For example, if the value is 3, the third line specified in the list is the destination of the branch.
The value of expression should be greater than or equal to 1 and less than or equal to the number of items in the list. If the value falls outside this range, one of the following results occurs:
Value | Result |
Number equal to 0 or greaterthan number of items in list | Control drops to the next BASIC statement. |
Negative number or number greater than 255 | An error message appears that reads "Illegal function call." |
You may mix line numbers and labels in the same list.
Note: | ON...GOTO statement accepts a maximum of 60 line labels or line numbers. The SELECT CASE statement provides a more powerful, convenient, and flexible way to do multiple branches. |
Example
This example causes program control to branch to one of three subroutines, depending on the value of Chval:
CLS ' Clear screen
Attend = 20
Fees = 5 * Attend
PRINT "1 Display attendance at workshops"
PRINT "2 Calculate total registration fees paid"
PRINT "3 End program"
PRINT : PRINT "What is your choice?"
Choice:
DO
ch$ = INKEY$
LOOP WHILE ch$ = ""
Chval = VAL(ch$)
IF Chval > 0 AND Chval < 4 THEN
ON Chval GOSUB Shop, Fees, Progend
END IF
END
Shop:
PRINT "ATTENDANCE IS", Attend
RETURN Choice
Fees:
PRINT "REGISTRATION FEES ARE $"; Fees
RETURN Choice
Progend:
END
See also:
Syntax
- ON expression% GOSUB line-label-list
- ON expression% GOTO line-label-list
Description/Parameter(s)
- The argument expression% is any numeric expression between 0 and 255, inclusive.
- The line-label-list consists of a list of line numbers or line labels, separated by commas. The value of expression% determines which line the program branches to. For example, if the value is 3, the third line specified in the list is the destination of the branch.
- The value of expression% should be greater than or equal to 1 and less than or equal to the number of items in the list. If the value falls outside this range, one of the following results occurs:
Value Result Number equal to 0 or greater than number of items in list Control drops to the next BASIC statement. Negative number or number greater than 255 BASIC generates the error message, "Illegal function call."
Usage Notes
- You can mix line numbers and labels in the same list.
- ON...GOTO statement accepts a maximum of 60 line labels or line numbers. The SELECT CASE statement provides a more powerful, convenient, and flexible way to do multiple branches.
Example
This example uses the ON...GOSUB statement to cause program control to branch to one of three subroutines, depending on the value of Chval.
CLS 'Clear screen.
Attend = 20
Fees = 5 * Attend
PRINT "1 Display attendance at workshops"
PRINT "2 Calculate total registration fees paid"
PRINT "3 End program"
PRINT : PRINT "What is your choice?"
Choice:
DO
ch$ = INKEY$
LOOP WHILE ch$ = ""
Chval = VAL(ch$)
IF Chval > 0 AND Chval < 4 THEN
ON Chval GOSUB Shop, Fees, Progend
END IF
END
Shop:
PRINT "ATTENDANCE IS", Attend
RETURN Choice
Fees:
PRINT "REGISTRATION FEES ARE $"; Fees
RETURN Choice
Progend:
END
See also: