IIf
Encyclopedia
In computing
, IIf (an abbreviation for Immediate if
) is a function in several editions of the Visual Basic
programming language
, related languages such as ColdFusion Markup Language
(CFML), and on spreadsheet
s that returns one of its three parameters
based on the evaluation of one of the other parameters. It is an example of a conditional expression
, which is similar to a conditional statement.
of the IIf function is as follows:
All three parameters are required:
Many languages have an operator
to accomplish the same purpose, generally referred to as a conditional operator (or, less accurately, as a ternary 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.
Furthermore, the data type
of its arguments is
Although TrueFunction is the function intended to be called,
Also consider this one:
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
This issue makes the IIf call less useful than the conditional operator. To solve this issue, Microsoft developers had considered converting
and short-circuiting by replacing the function call with inline code.
, 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.
Visual Basic 2008 (VB 9.0) introduced a true conditional operator, called simply "If", which also eliminates this problem. Its syntax is similar to the IIf function's syntax:
script, with similar syntax.
calling
and xBase
. (1992 and before.)
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 the
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...
, IIf (an abbreviation for Immediate if
Conditional statement
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false...
) is a function in several editions of the Visual Basic
Visual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...
programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
, related languages such as ColdFusion Markup Language
ColdFusion Markup Language
ColdFusion Markup Language, more commonly known as CFML, is a scripting language for web development that runs on the JVM, the .NET framework, and Google App Engine...
(CFML), and on spreadsheet
Spreadsheet
A spreadsheet is a computer application that simulates a paper accounting worksheet. It displays multiple cells usually in a two-dimensional matrix or grid consisting of rows and columns. Each cell contains alphanumeric text, numeric values or formulas...
s that returns one of its three parameters
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...
based on the evaluation of one of the other parameters. It is an example of a conditional expression
Conditional statement
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false...
, which is similar to a conditional statement.
Syntax
The syntaxSyntax (logic)
In logic, syntax is anything having to do with formal languages or formal systems without regard to any interpretation or meaning given to them...
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 an operator
Operator (programming)
Programming languages typically support a set of operators: operations which differ from the language's functions in calling syntax and/or argument passing mode. Common examples that differ by syntax are mathematical arithmetic operations, e.g...
to accomplish the same purpose, generally referred to as a conditional operator (or, less accurately, as a ternary operator); the best known is ?:
?:
In computer programming, ?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages...
, 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.Efficiency
BecauseIIf
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
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
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 withIIf
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.Also consider this one:
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
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 inferenceType inference
Type inference refers to the automatic deduction of the type of an expression in a programming language. If some, but not all, type annotations are already present it is referred to as type reconstruction....
and short-circuiting by replacing the function call with inline code.
Alternatives to IIf
In Visual BasicVisual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...
, 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
Conditional statement
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false...
s:
The above example would also eliminate the problem of IIf evaluating both its truepart and falsepart parameters.
Visual Basic 2008 (VB 9.0) introduced a true conditional operator, called simply "If", which also eliminates this problem. Its syntax is similar to the IIf function's syntax:
IIf in other programming languages
$iif
is also present in mIRCMIRC
mIRC is an Internet Relay Chat client for Microsoft Windows, created in 1995 and developed by Khaled Mardam-Bey. Although it is a fully functional chat utility, its integrated scripting language makes it extensible and versatile....
script, with similar syntax.
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 dBaseDBASE
dBase II was the first widely used database management system for microcomputers. It was originally published by Ashton-Tate for CP/M, and later on ported to the Apple II and IBM PC under DOS...
and xBase
XBase
xBase is the generic term for all programming languages that derive from the original dBASE programming language and database formats. These are sometimes informally known as dBASE "clones"...
. (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
Type inference
Type inference refers to the automatic deduction of the type of an expression in a programming language. If some, but not all, type annotations are already present it is referred to as type reconstruction....
) and the
iif
function will fill it depending on the outcome of the boolean expression.