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:

  • Data type: INTEGER
    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.
  • Data type: Identifier
    List Comma separated list of labels or subroutine names.

Example 1:

GLOBAL i
INPUT "Enter one number, 1, 2, or 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

Result:

Enter one number, 1, 2, or 3. 3
SUB Three
Subroutine Three
Label Three

Example 2:

GUI "TestApp"

SUB FORMLOAD
 GLOBAL Form1 AS CONTROL
 Form1 = BCX_FORM("Test Application")
 BCX_BUTTON("Test button #1", Form1, 201, 10,  10) ' <<<<< Ctrl = 201 
 BCX_BUTTON("Test button #2", Form1, 202, 10,  60) ' <<<<< Ctrl = 202 
 BCX_BUTTON("Test button #3", Form1, 203, 10, 110) ' <<<<< Ctrl = 203 
 CENTER(Form1)
 SHOW(Form1)
END SUB

BEGIN EVENTS
 SELECT CASE CBMSG
 CASE WM_COMMAND
  IF CBCTLMSG = BN_CLICKED THEN
   '******************************************************* 
   DIM RAW i = CBCTL - 200 '<<< ctrl 201, 202, 203 ->> 1, 2, 3 
   ON i CALL OnClick1(i), OnClick2(i), OnClick3(i)
   '******************************************************* 
  END IF
 END SELECT
END EVENTS

SUB OnClick1 (a) '<< Shows using a parameter 
 MSGBOX("Hello from click" + STR$(a))
END SUB

SUB OnClick2 (a) '<< Shows using a parameter 
 MSGBOX("Hello from click" + STR$(a))
END SUB

SUB OnClick3 (a) '<< Shows using a parameter 
 MSGBOX("Hello from click" + STR$(a))
END SUB

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