CHOOSE function

Purpose

Return one of several values, based upon the value of an index.

Syntax

y  = CHOOSE(index&, choice1 [, choice2] ...)
y&
= CHOOSE&(index&, choice1 [, choice2] ...)
y$
= CHOOSE$(index&, choice1 [, choice2] ...)

Remarks

These functions take any number of choice arguments, and return the argument identified by index&.  If index& evaluates to one, choice1 is returned, if two, choice2 is returned, etc.  If index& is less than one or greater than the number of choices provided, zero or an empty string is returned accordingly.

CHOOSE expects choices of any numeric type.  CHOOSE& expects choices optimized for the Long-integer type.  CHOOSE$ expects choices of string type. CHOOSE% is recognized as a valid synonym for CHOOSE&.

Restrictions

PowerBASIC only evaluates the selected choice at run-time, not all of them.  This ensures optimum execution speed, as well as the elimination of unanticipated side effects.

See also

IIF, IIF&, IIF$, MAX, MAX&, MAX$, MIN, MIN&, MIN$, SWITCH, SWITCH&, SWITCH$, SELECT

Example

y& = 4

a$ = CHOOSE$(y&, "Bill", "Bob", "Bruce", "Barry")

Result

a$ = "Barry"