Skip to content

On ... Gosub Command

Obsolete

This command is replaced by the Case command.

Purpose

To select one of a series of subroutines to execute, based on the numeric value of a variable or expression.

Syntax

on
variable
gosub
label [...label]

One label is required, and additional labels may be provided, separated by spaces. Each label must exist in the program.

What It Does

The numeric value of variable is used as an index to select for execution one of the subroutines whose label is listed. If the value of variable is 1, the first label is executed; if the value of variable is 2, the second is executed, and so forth. When the subroutine has been executed, execution returns to the command line next after the on ... gosub command.

If the value of variable is less than 1 or greater than the number of labels listed, no subroutine will be executed.

on time gosub early on_time late

In this example, when the value of time is 1, DataFlex executes subroutine early. When the value is 2, it executes on_time, and when it is 3, it does late. When time has any other value, execution passes to the line after on ... gosub.

Notes

  • You must provide a subroutine under label for every label in every on ... gosub command you use. The labelled routine must end with a return command.

  • If the value of variable is less than 1 or greater than the total number of labels on the on ... gosub command line, no subroutine will be executed. If you want this to constitute an error in your program, declare the error as follows:

    on music gosub rock country jazz
    if music gt 3 error 198 "INVALID MUSIC TYPE SELECTED"
    

    If the value of music is greater than 3, DataFlex declares Error 198.

  • You may precede on ... gosub with indicators and if tests.

  • The on ... goto command is similar to the on ... gosub command with the exception that the on ... goto command does not return to the line after the on ... goto command.

  • If variable is a number that includes a fractional (decimal) value, the value will be truncated to an integer value before the on ... gosub selection is made.