Function If() | IIf() Foundation

Returns the result of an expression dependent on a logical expression.

Syntax
If( <lCondition>, <ExprTrue>, <ExprFalse>) --> xValue
IIf(<lCondition>, <ExprTrue>, <ExprFalse>) --> xValue
Parameters
<lCondition>
<lCondition> is a logical expression whose value determines the return value of If().
<ExprTrue>
<ExprTrue> is the return value of If() when <lCondition> is .T. (true). It can be an expression of any data type.
<ExprFalse>
<ExprFalse> is the return value of If() when <lCondition> is .F. (false). It can be an expression of any data type. The data types of <ExprTrue> and <ExprFalse> can be different.
Return

When <lCondition> returns the value .T. (true), the return value of If() is the value of <ExprTrue>, otherwise it is the value of <ExprFalse>. The data type of the return value is the same as the type of the selected partial expression.

Description

The function If() first evaluates the logical expression <lCondition>. Whether <lCondition> provides the value .T. or .F., a second expression is evaluated and the value of this expression is the return value of If(). This function is one of the most efficient and versatile Xbase++ functions. Using it, logical expressions can be converted to any other data type. The function can be written as If() or IIf().

If()/IIf() is an inline function that is translated into code at compile time. It is not a function in the runtime library XPPRT1.DLL and cannot be overloaded.

Examples
If()
// This example converts a numeric value to a character 
// string and displays a character string in one of two 
// different colors depending on the numeric value. 

PROCEDURE Main 
   LOCAL nValue := 0, cString 
   CLS 

   @ 0,0 SAY "Input number:" GET nValue PICTURE "9999.99" 
   READ 

   cString := IIf( nValue<0, "Negative number", "Positive number" ) 

   @ 1,0 SAY cString COLOR IIf( nValue<0, "W+/R", "W+/N" ) 

RETURN 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.