ON ... GOSUB, ON ... GOTO, ON ... CALL statement

Purpose: These are control flow statements that will branch to a line label or subroutine depending on the value of a number.


 Syntax 1:

 ON Number GOSUB List

 Syntax 2:

 ON Number GOTO List

 Syntax 3:

 ON Number CALL List

 Parameters:

  • Number Integer literal or variable that corresponds to the position in the list of the label or subroutine name. The value of Number determines to which label or subroutine in the list the program branches.For example, if the value is 2, the label specified in the second position in the list is the destination of the branch.
  • List Comma separated list of labels or subroutine names.

Here is a complete example.


 GLOBAL i
 INPUT "Enter a number(1,2,3) " , i

 ON i CALL   Sub_one, Sub_two, Sub_three
 ON i GOSUB  One, Two, Three
 ON i GOTO   Label_One, Label_Two, Label_Three

 END ' To stop the program from flowing to the code below

 Label_One:
 PRINT  "Label One"
 END

 Label_Two:
 PRINT  "Label Two"
 END

 Label_Three:
 PRINT  "Label Three"
 END

 One:
 PRINT  "Subroutine One"
 RETURN

 Two:
 PRINT  "Subroutine Two"
 RETURN

 Three:
 PRINT  "Subroutine Three"
 RETURN

 SUB  Sub_one
  PRINT  "SUB One"
 END SUB

 SUB  Sub_two
  PRINT  "SUB Two"
 END SUB

 SUB  Sub_three
  PRINT  "SUB Three"
 END SUB

BCX Console Sample Programs using ON ... GOSUB, ON ... GOTO, ON ... CALL function.

S134.bas