Investment Studio > Expressions > Functions > Math & Trig > IF

if (boolean condition, true_result = TRUE, false_result = FALSE)

Returns true_result if the condition is TRUE, false_result if the condition is FALSE.

true_result and false_result can be values of any type (including arrays) and determine the result type of the function.

Either true_result, false_result or both can be omitted, in which case they default to the boolean values TRUE and FALSE.

To build an error-tolerant IF expression, compute the value of each possible result separately, assign the results to symbols, then use the symbols in the IF expression.

This way, errors occurring during result evaluation will not prevent the IF expression from being evaluated. This is different from evaluating the results and IF in the same expression because expression evaluation is aborted when it results in an error; if the results are evaluated in the same expression as IF, an error occurring during the evaluation of either element of the result list will prevent the entire IF expression from being evaluated.

The condition can also be made error-tolerant using the OR operator.

Examples

=if(1 < 2)

equals TRUE (default true_result)

=if(3 < 2)

equals FALSE (default false_result)

=if(1 < 2, "Smaller")

equals "Smaller" (explicit true_result)

=if(3 < 2, "Smaller")

equals FALSE (default false_result)

=if(1 < 2, , "Larger")

equals TRUE (default true_result)

=if(3 < 2, , "Larger")

equals "Larger" (explicit false_result)

=if(1 < 2, {3, 4, 5}, "Larger")

equals {3, 4, 5} (explicit true_result)

=if(3 < 2, {3, 4, 5}, "Larger")

equals "Larger" (explicit false_result).

See also logical operators AND, OR, XOR, NOT.