# Ternary Analytics Operations

> There are two ternary analytics operations iif and if that take three input sequences, cond, then and else, and produce a sequence of values that represent the result of an If-Then-Else condition. When the boolean value in sequence cond is true, an element from sequence then is selected; when false from sequence else. However, the positions in sequence 2 and 3 are advanced differently. For iif the position in both is advanced to correspond with the position in sequence cond; for if the position of only the “selected” sequence is advanced.

> To illustrate, suppose we have the following data sets in the three input sequences:

>         cond: {0,1,0,0,1}
>         then: {-1,-2,-3,-4,-5}
>         else: {1,2,3,4,5}

> The result sequence for ``mco_seq_iif_TYPE()`` with these data sets is obtained as indicated in the following table:

> cond | then | else | result
> --- | --- | --- | ---
> 0 | -1 | 1 | 1
> 1 | -2 | 2 | -2
> 0 | -3 | 3 | 3
> 0 | -4 | 4 | 4
> 1 | -5 | 5 | -5

> For `mco_seq_if_TYPE()`:

> cond | then | else | result
> --- | --- | --- | ---
> 0 | -1 | 1 | 1
> 1 | -2 | 2 | -1
> 0 | -3 | 3 | 2
> 0 | -4 | 4 | 3
> 1 | -5 | 5 | -2

> ## Examples

> ### C API

> The C function signatures are of the following form:

>         MCO_RET mco_seq_operator_TYPE(mco_seq_iterator_h result,
>                 mco_seq_iterator_h cond,
>                 mco_seq_iterator_h then_iter,
>                 mco_seq_iterator_h else_iter)

> where TYPE is one of the types listed in the Analytics Functions page and operator is iif or if.

> Following is an example code snippet demonstrating a C ternary function:

>         {
>             mco_seq_iterator_t result, cond_iterator, then_iterator, else_iterator;
>             MCO_RET rc;
>             ...
>             rc = mco_seq_iif_float(&result, &cond_iterator, &then_iterator, &else_iterator);

>             ...
>         }

> ### Python Methods

> The Python ternary methods are iif() and if(). Following is an example code snippet demonstrating a Python ternary method:

>         cursor = con.cursor("Quote", "by_sym")
>         for quote in cursor:
>             ...
>             openit,closeit = dayit.project('open', 'close')
>             gtit = quote.close.gt(quote.open)
>             iffit = gtit.iff(quote.close,quote.open)
>             ...

> ### SQL Functions

>         INSERT INTO SimpleSequence(testNumber,bVal1,iVal2,iVal3)
>             VALUES(1,'{0,1,0,0,1}','{-1,-2,-3,-4,-5}','{1,2,3,4,5}');

>         SELECT bVal1,iVal2,iVal3,seq_iif(bVal1,iVal2,iVal3) AS "iif" FROM SimpleSequence
>         WHERE testNumber=1;
>         SELECT bVal1,iVal2,iVal3,seq_if(bVal1,iVal2,iVal3) AS "if" FROM SimpleSequence
>         WHERE testNumber=1;

>         bVal1{}
>         iVal2{}
>         iVal3{}
>         iif{}
>         -------------------------------------------------------------------------
>         {0, 1, 0, 0, 1}
>         {-1, -2, -3, -4, -5}
>         {1, 2, 3, 4, 5}
>         {1, -2, 3, 4, -5}

>         bVal1{}
>         iVal2{}
>         iVal3{}
>         if{}
>         -------------------------------------------------------------------------
>         {0, 1, 0, 0, 1}
>         {-1, -2, -3, -4, -5}
>         {1, 2, 3, 4, 5}
>         {1, -1, 2, 3, -2}