xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

If() | IIf()

Returns the result of an expression based on a logical expression

Syntax

If( <lCondition>, <exprTrue>, <exprFalse> ) --> xValue
or
IIf(<lCondition>, <exprTrue>, <exprFalse> ) --> xValue

Arguments

<lCondition>
This is a logical expression which determines the return value of If().
<exprTrue>
An expression resulting in any data type. It is evaluated when <lCondition> is .T. (true).
<exprFalse>
An expression resulting in any data type. It is evaluated when <lCondition> is .F. (false).

Return

The function returns the value of <exprTrue> if <lCondition> is .T. (true), otherwise the value of <exprFalse> is returned.

Description

Function If(), and its syntactical synonym IIf(), is a conditional function providing two possible return values. It first evluates the expression <lCondition>. Based on this result, it returns the value of either <exprTrue> or <exprFalse>.

Info

See also:DO CASE, IF, SWITCH
Category: Conversion functions , Logical functions
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example sorts a two column array. IIf() compares values
// in the second column when adjacent array elements in the
// first column contain identical values.


   PROCEDURE Main
      LOCAL aArray := { ;
            { "B", 3 }, ;
            { "A", 2 }, ;
            { "B", 1 }, ;
            { "C", 4 }, ;
            { "A", 1 }  }

      ASort( aArray,,, {|a,b| IIf( a[1] == b[1], ;
                                   a[2] <  b[2], ;
                                   a[1] <  b[1]  ) } )

      AEval( aArray, {|a| Qout( ValToPrg(a) ) } )
      // result:
      // { "A", 1 }
      // { "A", 2 }
      // { "B", 1 }
      // { "B", 3 }
      // { "C", 4 }
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe