Xbasic

# CASE Function

Syntax

Value as A = CASE(condition_1 as L, value_1 as A, [condition_N as L, value_N as A])

Arguments

condition_1Logical

Logical. An expression that evaluates to .T. (TRUE) or .F. (FALSE).

value_1Any Type

The value returned if condition_1 evaluates to .t.

condition_NLogical

Additional conditional statement that evaluates to .T. (TRUE) or .F. (FALSE). A maximum of 15 additional conditional expressions can be specified.

value_NAny Type

The corresponding values returned for the specified condition. A result is required for each condition specified.

Returns

ValueAny Type

Returns the value that for the first condition that evaluates to .t..

Description

Returns the corresponding value for the first condition that evaluates to true (.t.)

Discussion

CASE() returns the value (Value_1 ... Value_N) specified by the first expression (Condition_1...Condition_N) that evaluates to .T. (TRUE). A maximum 16 cases can be specified.

The CASE() function can be used to specify filter and order statements for searching and sorting records.

### Example: Basic Example

```dim TEMP as N
TEMP = 30

? case(TEMP < 32, "Cold", TEMP < 65, "Cool", TEMP < 80, "Warm", TEMP >= 80, "Really Hot")
= "Cold"

TEMP = 75
? case(TEMP < 32, "Cold", TEMP < 65, "Cool", TEMP < 80, "Warm", TEMP >= 80, "Really Hot")
= "Warm"

TEMP = 90
? case(TEMP < 32, "Cold", TEMP < 65, "Cool", TEMP < 80, "Warm", TEMP >= 80, "Really Hot")
= "Really Hot"```

### Example: Filter Records

If you wanted to filter records for a limited number of specific values, you use this type of expression, which will examine the firstname field and select records containing the values "John", "Peter", and "Sally".

`case( "John" \$ firstname, .t., "Peter" \$ firstname, .t., "Sally" \$ firstname, .t. )`

### Example: Sorting Records

You can create an explicit order for records using the CASE() function. For example:

`case(containsi("Jan Feb Mar",month), 1, containsi("Apr May Jun",month), 2, containsi("Jul Aug Sep",month), 3, containsi("Oct Nov Dec",month), 4)`