IIf


In computing, IIf is a function in several editions of the Visual Basic programming language and ColdFusion Markup Language, and on spreadsheets that returns the second or third parameter based on the evaluation of the first parameter. It is an example of a conditional expression, which is similar to a conditional statement.

Syntax

The syntax of the IIf function is as follows:
IIf
All three parameters are required:
Many languages have an operator to accomplish the same purpose, generally referred to as a conditional operator ; the best known is ?:, as used in C, C++, and related languages. Some of the problems with the IIf function, as discussed later, do not exist with a conditional operator, because the language is free to examine the type and delay evaluation of the operands, as opposed to simply passing them to a library function.

Examples

These examples evaluate mathematical expressions and return one of two strings depending on the outcome.
result = IIf ' Returns "Yes it is"
result = IIf ' Returns "Wrong"

Criticisms

Efficiency

Because IIf is a library function, it will always require the overhead of a function call, whereas a conditional operator will more likely produce inline code.
Furthermore, the data type of its arguments is Variant. If the function is called with arguments of other types, there will be additional overhead to convert these to Variant. There may also be additional overhead to check the argument types and convert one of them if they do not have the same type.

Side effects

Another issue with IIf arises because it is a library function: unlike the C-derived conditional operator, both truepart and the falsepart will be evaluated regardless of which one is actually returned. In the following code snippet:

value = 10
result = IIf

although TrueFunction is the function intended to be called, IIf will call both TrueFunction and FalseFunction. Similarly,

a = 10
b = 0
result = IIf

While the intent may be to avoid a division by zero, whenever b is zero the error will actually happen. This is because the code in the snippet is executed as if by

a = 10
b = 0
_temp1 = 0
_temp2 = a / b ' Error if b = 0
_temp3 = b <> 0
If _temp3 Then
result = _temp1
Else
result = _temp2
End If

This issue makes the IIf call less useful than the conditional operator. To solve this issue, Microsoft developers had considered converting IIf to an intrinsic function; had this happened, the compiler would have been able to perform type inference and short-circuiting by replacing the function call with inline code.

Alternatives to IIf

In Visual Basic, IIf is not the sole way to evaluate and perform actions based on whether an expression is true or false.
The following example uses IIf:
result = IIf
It could also be written in the following way, using standard conditionals:

If x = y Then
result = value1
Else
result = value2
End If

The above example would also eliminate the problem of IIf evaluating both its truepart and falsepart parameters.
Visual Basic 2008 introduced a true conditional operator, called simply "If", which also eliminates this problem. Its syntax is similar to the IIf function's syntax:
result = If

IIf in other programming languages

$iif is present in mIRC script, with similar syntax.

alias testiif
alias testiif2

Calling /testiif will print out "testing $iif: 1 execution". mIRC's $iif acts more like C's ?: than IIf in VB since it won't pre-evaluate both.
IIF is a function in dBase and xBase.
iif is also a compiler magic function of Oxygene. It is not a real function and is at compile time unrolled to conditional statements.

var someString := iif;

In this example a new strong type string named "someString" is created and the iif function will fill it depending on the outcome of the boolean expression.
SQL Server 2012 and newer implements the IIF function:

DECLARE @a int = 45;
DECLARE @b int = 40;
SELECT IIF AS Result;

IIf in C is the :

printf;

IIf in Python:

parity = 'odd' if n%2 else 'even'

IIf in Red and Rebol:

parity: either odd? n