- IIf
In
computing , IIf (an abbreviation for Immediate if [cite web
url=http://support.microsoft.com/kb/209192 |title=How to Use the IIf() (Immediate If) Function |accessdate=2007-05-09 |date=2004-06-08] ) is a function in several editions of theVisual Basic programming language , related languages such asColdFusion Markup Language , and onspreadsheet s 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.yntax
The syntax of the IIf function is as follows:
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 operator s; 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.
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 isVariant
. If the function is called with arguments of other types (variables or literals), there will be additional overhead to convert these toVariant
s. There may also be additional overhead to check the argument types and convert one of them if they do not have the same type.ide 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:Although "TrueFunction" is the function intended to be called,
IIf
will cause both "TrueFunction" and "FalseFunction" to be executed.Microsoft developers have mooted [cite web
url=http://www.panopticoncentral.net/archive/2006/12/29/18883.aspx |title=IIF, a True Ternary Operator and Backwards Compatibility |accessdate=2007-02-01 |author=Paul Vick |date=2006-12-29] convertingIIf
to an intrinsic function; were this to happen, the compiler would be able to performtype 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:
It could also be written in the following way, using standard
conditional statement s: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 inmIRC script, with similar syntax.calling
/testiif
will print out "testing $iif: 1 execution(s). mIRC's$iif
acts more like C's?:
thanIIf()
in VB since it won't pre-evaluate both.IIF()
is a function indBase andxBase . (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.In this example a new strong type string named "someString" is created (using
Type inference ) and theiif
function will fill it depending on the outcome of the boolean expression.References
External links
* [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctIIF.asp MSDN Documentation for IIf]
Wikimedia Foundation. 2010.