ON…GOSUB…

Function

ON…GOSUB… allows conditional subroutine calls.

Syntax

ON expression GOSUB line number 1, line number 2, ….line number n.

Examples

10 ON A GOSUB100,180
20 PRINT "HELLO"

If A=1 then program execution will go to line 100 and will then go to line 20 when a RETURN is encountered.

If A=2 then program execution will go to line 180 and will then go to line 20 when a RETURN is encountered.

10 B=100:C=300;D=350
20 ON A GOSUB B,C,D
30 PRINT "HELLO"

If A=1 then program execution will go to line 100 and revert back to line 30 when a RETURN is encountered.

If A=3 then program execution will go to line 350 and revert back to line ?0 when a RETURN is encountered.

Remarks

ON GOSUB will cause program execution to go to a line number chosen from the list of line numbers positioned immediately after GOSUB. The selection of a particular line number in this list is determined by the value of [expression].

Once program execution has been forced to go to selected one of the line numbers, each statement following that line number will be executed in order until a RETURN is encountered. At this point program execution will revert back to the statement following the list of line numbers.

The integer part of [expression] is used so that if A=2.9 program execution will go to line 180 in the example.

Note

The value of [expression] is NOT rounded up.

If the value of [expression] is zero or greater than the number of line numbers in the list, then program execution will continue with the next statement.

In the above example, if A=3 then “HELLO” will be printed IMMEDIATELY.

The maximum value of [expression] allowed is 255.

If the value of [expression] is greater than 255 or negative then a ‘Magnitude error’ will occur.

The line numbers in the list of line numbers may themselves be expressions.