back to Command Reference
[All Models]
Jumps to a specified subroutine in accordance with a specified branching condition.
ON condition GOSUB [ branch [, [ branch
└───┬───┘ destination ] destination ] ]*
Numeric expression
Branch destination:
destination branch line number
└─────────────┬──────────────┘
Line number
# program area number
└────────┬────────┘
Single character; 0~9
ON A GOSUB 1000, 1100, 1200
Branch destinations can specify a program area number using the #n syntax
(e.g., #2) to call subroutines in other program areas. When using cross-program-area
branching, the subroutine in the target program area must contain a RETURN
statement to return control to the calling program.
10 S1=0: S2=0
20 FOR I=1 TO 100
30 ON (I MOD 2)+1 GOSUB 1000, 1100
40 NEXT I
50 PRINT "S1="; S1
60 PRINT "S2="; S2
70 END
1000 S1=S1+I: RETURN
1100 S2=S2+I: RETURN
S1 calculates sum of even numbers from 1 to 100, S2 calculates sum of odd numbers from 1 to 100.