IIf
   HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, e ...
, IIf (an abbreviation for Immediate if) is a
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-oriente ...
in several editions of the
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic (cl ...
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
and
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. Multiple commercial and open source implementations of CFML engines are availab ...
(CFML), and on
spreadsheet A spreadsheet is a computer application for computation, organization, analysis and storage of data in tabular form. Spreadsheets were developed as computerized analogs of paper accounting worksheets. The program operates on data entered in cel ...
s that returns the second or third
parameter A parameter (), generally, is any characteristic that can help in defining or classifying a particular system (meaning an event, project, object, situation, etc.). That is, a parameter is an element of a system that is useful, or critical, when ...
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 In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure ( constituency) ...
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 an operator to accomplish the same purpose, generally referred to as a
conditional operator The conditional operator is supported in many programming languages. This term usually refers to ?: as in C, C++, C#, and JavaScript. However, in Java, this term can also refer to && and , , . && and , , In some programming languages, e.g. Jav ...
(or, less precisely, 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.


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 In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
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. In the following code snippet: value = 10 result = IIf(value = 10, ''TrueFunction'', FalseFunction) although ''TrueFunction'' is the function intended to be called, IIf will call both ''TrueFunction'' and ''FalseFunction''. Similarly, a = 10 b = 0 result = IIf(b <> 0, a / b, 0) 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 = b <> 0 _temp2 = a / b ' Error if b = 0 _temp3 = 0 If _temp1 Then result = _temp2 Else result = _temp3 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 Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistics ...
and short-circuiting by replacing the function call with inline code.


Alternatives to IIf

In
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic (cl ...
, 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(x = y, value1, value2) 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 (VB 9.0) introduced a true
conditional operator The conditional operator is supported in many programming languages. This term usually refers to ?: as in C, C++, C#, and JavaScript. However, in Java, this term can also refer to && and , , . && and , , In some programming languages, e.g. Jav ...
, called simply "If", which also eliminates this problem. Its syntax is similar to the IIf function's syntax: result = If(x = y, value1, value2)


IIf in other programming languages

$iif() is present in
mIRC mIRC is an Internet Relay Chat (IRC) client for Windows, created in 1995. It is a fully functional chat utility and its integrated scripting language makes it extensible and versatile. mIRC has been described as "one of the most popular IRC cl ...
script, with similar syntax.
alias 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 dBase (also stylized dBASE) was one of the first database management systems for microcomputers and the most successful in its day. The dBase system includes the core database engine, a query system, a forms engine, and a programming language ...
and
xBase xBase is the generic term for all programming languages that derive from the original dBASE ( Ashton-Tate) programming language and database formats. These are sometimes informally known as dBASE "clones". While there was a non-commercial predec ...
(1992 and older). 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 Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistics ...
) 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 ( @a > @b, 'TRUE', 'FALSE' ) AS Result; IIf in C is the ?: conditional operator: printf("number %d is%s even", num, num % 2 ? " not" : ""); IIf in Python: parity = "odd" if n % 2 else "even" IIf (either) in Red and Rebol: parity: either odd? n
odd Odd means unpaired, occasional, strange or unusual, or a person who is viewed as eccentric. Odd may also refer to: Acronym * ODD (Text Encoding Initiative) ("One Document Does it all"), an abstracted literate-programming format for describing X ...
'even]


References


External links


MSDN Documentation for IIf
Microsoft Visual Studio Conditional constructs Articles with example Python (programming language) code Articles with example BASIC code BASIC programming language