IIf
In computing, IIf (an abbreviation for Immediate if [1]) is a function in several editions of the Visual Basic programming language, related languages such as ColdFusion Markup Language (CFML), and on spreadsheets that returns one of its two parameters based on the evaluation of an expression. 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(expr, truepart, falsepart)
All three parameters are required:
- expr is the expression that is to be evaluated.
- truepart defines what the IIf function returns if the evaluation of expr returns true.
- falsepart defines what the IIf function returns if the evaluation of expr returns false.
Many languages have operators to accomplish the same purpose, generally referred to as ternary operators; 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 ternary operators, 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(5 < 10, "Yes it is", "No it isn't") ' Returns "Yes it is"
result = IIf(2 + 2 = 5, "Correct", "Wrong") ' 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 (variables or literals), 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. Consider the following example:
value = 10
result = IIf(value = 10, TrueFunction, FalseFunction)
Although TrueFunction is the function intended to be called, IIf
will cause both TrueFunction and FalseFunction to be executed.
Also consider this one:
a = 10
b = 0
result = IIf(b <> 0, a / b, 0)
While the programmer intends to avoid raising an error by performing a division by zero, whenever b is zero the error will actually happen. This is because the code in the snippet is to be read as
a = 10
b = 0
_temp1 = a / b ' Error if b = 0
_temp2 = 0
_temp3 = b <> 0
result = IIf(_temp3, _temp1 , _temp2)
This issue makes the IIf() call less useful than the ternary operator. To solve this issue, Microsoft developers had considered[2] 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(test Mod 4 = 0, DoSomething, DoSomethingElse)
It could also be written in the following way, using standard conditional statements:
If test Mod 4 = 0 Then
result = DoSomething
Else
result = DoSomethingElse
End If
The above example would also eliminate the problem of IIf evaluating both its truepart and falsepart parameters.
IIf in other programming languages
$iif()
is also present in mIRC script, with similar syntax.
alias testiif {
%testiif = 0
echo -a $iif(1,$testiif2,$testiif2) %testiif execution(s)
unset %testiif
}
alias testiif2 { inc %testiif | return testing $!iif: }
calling /testiif
will print out "testing $iif: 1 execution(s). 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. (1992 and before.)
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(someInt > 35 , 'Large', 'Small');
In this example a new strong type string named "someString" is created (using Type inference) and the iif
function will fill it depending on the outcome of the boolean expression.
References
- ↑ "How to Use the IIf() (Immediate If) Function". 2004-06-08. http://support.microsoft.com/kb/209192. Retrieved 2007-05-09.
- ↑ Paul Vick (2006-12-29). "IIF, a True Ternary Operator and Backwards Compatibility". http://www.panopticoncentral.net/archive/2006/12/29/18883.aspx. Retrieved 2007-02-01.
External links
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...