Skip to content

On ... Goto Command

Obsolete

This command is replaced by the Case command.

Purpose

To select one of a series of labels to pass execution to, based on the numeric value of a variable or expression.

Syntax

on
variable
goto
label [...label]

Argument Explanation

  • variable: May not be a constant or of Type String, and must have positive numeric values.
  • label: Required, and additional labels may be provided, separated by spaces.

What It Does

The numeric value of variable is used as an index to select one of the listed labels to pass execution to. If the value of variable is 1, execution is passed to the first label; if the value of variable is 2, execution passes to the second, and so forth.

If the value of variable is less than 1 or greater than the number of labels listed, execution will go to the line following the on ... goto command.

on time goto early on_time late

In this example, when the value of variable time is 1, DataFlex passes execution to label early. When the value is 2, it goes to on_time, and when it is 3, it goes to late. When time has any other value, execution passes to the line after on ... goto.

Notes

  • You must provide a label in your program for every label in every on ... goto command you use.

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

    on music goto 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 ... goto 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 which includes a fractional (decimal) value, the value will be truncated to an integer value before the on ... goto selection is made.