CHOOSE function

Purpose | Return one of several values, based upon the value of an index.
Syntax | y = CHOOSE(index&, choice1 [, choice2] ...[ELSE choice9]) y& = CHOOSE&(index&, choice1 [, choice2] ...[ELSE choice9]) y$ = CHOOSE$(index&, choice1 [, choice2] ...[ELSE choice9]) y = CHOOSE([BIT] index&, choice1 [, choice2] ...[ELSE choice9]) y& = CHOOSE&([BIT] index&, choice1 [, choice2] ...[ELSE choice9]) y$ = CHOOSE$([BIT] index&, choice1 [, choice2] ...[ELSE choice9]) y$ = CHOOSE$([BITS] index&, choice1 [, choice2] ...[ELSE choice9])
Remarks | These functions may take any number of choice parameters. They return one of the parameters, or a combination of them, based upon the value of index&. In the standard form, index& makes the choice based upon the sequence of the parameters. That is, if index& is one, choice1 is returned. If two, choice2 is returned, etc. If index& is not equal to one of the choice values, the default value is returned to the calling code. CHOOSE expects choices of any numeric type. CHOOSE& expects choices optimized for an integral data type. CHOOSE$ expects choices of string type. CHOOSE% is recognized as a valid synonym for CHOOSE&.
ELSE | If no match is made with one of the choice values, the value zero (0) or an empty (zero-length) string is normally returned. However, if an ELSE clause is included as the last choice, its value is returned as the default value. For example: ChoiceVar$ = CHOOSE$(7,"ONE", "TWO" ELSE "NUL") In this case, the ELSE expression "NUL" is returned.
BIT | If the BIT option is included, the selection is based upon the first bit set (lowest to highest) in index&. That is, the lowest bit (1) returns choice1, the next bit (2) returns choice2, the next bit (4) returns choice3, the next bit(8) returns choice4, etc. Evaluation of index& stops as soon as one set bit is found. This is particularly valuable when used with an ENUMERATION which also uses the BIT option, to describe a set of attributes for an item in your program.
BITS | This is similar to the BIT option, but is only available with the CHOOSE$() version. index& is evaluated in the same general fashion, but the function may return multiple choices, as a concatenated string, if more than one bit is set. For example: x$ = CHOOSE$(BITS 5, "Computer ", "Laptop ", "Desktop ") Since the value 5 consists of 2 bits (the lowest and third-lowest) set, the first and third strings are concatenated and returned to the caller. In this case, "Computer Desktop " is the result.
Restrictions | PowerBASIC only evaluates the selected choice(s) 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"