Xbasic

IIF Function

Syntax

Value as A = IIF(L condition,A result_true,A result_false)

Arguments

condition

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

result_true

A value.

result_false

A value of the same type as True_Value (i.e. character, date, logical, memo, or numeric).

Description

Tests an expression and returns one of two expressions.

Discussion

IIF() evaluates the Logical_Expression. If the expression evaluates to TRUE, the True_Value is returned, otherwise the False_Value is returned. For example: IIF(.T., "ABCDEF", "XYZ") ?ABCDEF". Note : Use IIF() instead of IF() when using the expression in an Xbasic script.

Example

If STATE contains "MA"

```? iif(STATE = "MA", .05, 0)
= .05```

If PAID (a logical field) is TRUE

```? iif(PAID, "Paid", "Outstanding")
= "Paid",```

Suppose that your company offers a discount based on the total amount of goods purchased. Customers buying over \$1,000 worth of merchandise receive a 10% discount. The amount of the order before the discount is kept in a field called SUBTOTAL. You can make DISCOUNT a calculated field, using the following expression:

`iif(SUBTOTAL > 1000, .1, 0)`

Nested if Statements

You can nest IIF() statements to create more complex tests. For example, assume that you use codes to save data entry time when entering college students into a table. The field CLASS contains 1 for "Freshman", 2 for "Sophomore", 3 for "Junior", and 4 for "Senior". To print a report that contains the year of school a student is in, rather than the code, define and place a calculated field called GRADE, which uses the following expression:

`iif(class=4, "Senior", iif(class=3, "Junior",  iif(class=2, "Sophomore", "Freshman")))`

If the CLASS is 4, then the TRUE action is taken and the expression returns "Senior." If the first test is FALSE, then the false action is taken. The false action in this case is another IIF() statement. The second IIF() tests CLASS to see if it is 3. If TRUE, then the expression returns "Junior." However, if the second test also fails, then the third IIF() is evaluated. The third IIF() tests to see if class=2. If so, the expression returns "Sophomore." If the third test fails the false action is taken. At this point, you have already tested for the other possibilities, so no further test is necessary. The student must be a "freshman."