IIf

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 the Visual Basic programming language, related languages such as ColdFusion Markup Language, 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.

yntax

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 Variants. 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:

value = 10result = IIf(value = 10, TrueFunction, FalseFunction)

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
] converting IIf to an intrinsic function; were this to happen, the compiler would be 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 = DoSomethingElse result = DoSomethingElseEnd 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

External links

* [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctIIF.asp MSDN Documentation for IIf]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • IIF — may refer to: * Institute of International Finance (IIF), an association of international financial institutions * Intuit Interchange Format, a file format used by Intuit s Quickbooks software * IIf, the computing function * Information… …   Wikipedia

  • IIF — ist die Abkürzung für: Institut für Industriekommunikation und Fachmedien, ein Forschungsinstitut der RWTH Aachen Institute of International Finance, eine globale Interessensvertretung von rund 420 Großbanken, derzeit unter dem Vorsitz von… …   Deutsch Wikipedia

  • IIF — Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom.   Sigles d’une seule lettre   Sigles de deux lettres > Sigles de trois lettres   Sigles de quatre lettres …   Wikipédia en Français

  • IIF —    Institute for International Finance. A global association of leading banks and financial institutions. Created in 1983 in response to an international debt crisis, the IIF analyses risks in emerging market economies, serves as a policy forum… …   Financial and business terms

  • IIF — Intuit Interchange Format (Computing » File Extensions) * Interchange File (QuickBooks for Windows) (Computing » File Extensions) * Morgan Stanley Dean Witter India Investment Fund, Inc. (Business » NYSE Symbols) …   Abbreviations dictionary

  • IIF — immune interferon; indirect immunofluorescence; intracellular ice formation; isolated intraperitoneal fluid …   Medical dictionary

  • IIF — Image Interchange Format, ISO/IEC 12087 3 Standard …   Acronyms

  • IIF — Image Interchange Format, ISO/IEC 12087 3 Standard …   Acronyms von A bis Z

  • IIF — abbr. If I Fail …   Dictionary of abbreviations

  • IIF — • immune interferon; • indirect immunofluorescence; • intracellular ice formation; • isolated intraperitoneal fluid …   Dictionary of medical acronyms & abbreviations

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”