词条 | IIf |
释义 |
In computing, IIf (an abbreviation for Immediate if[1]) is a function in several editions of the Visual Basic programming language and ColdFusion Markup Language (CFML), and on spreadsheets that returns the second or third parameter based on the evaluation of the first parameter. It is an example of a conditional expression, which is similar to a conditional statement. SyntaxThe syntax 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 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. ExamplesThese examples evaluate mathematical expressions and return one of two strings depending on the outcome. CriticismsEfficiencyBecause Furthermore, the data type of its arguments is Side effectsAnother issue with value = 10 result = IIf(value = 10, TrueFunction, FalseFunction) although TrueFunction is the function intended to be called, 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 = 0 _temp2 = a / b ' Error if b = 0 _temp3 = b <> 0 If _temp3 Then Else End If This issue makes the IIf() call less useful than the conditional operator. To solve this issue, Microsoft developers had considered[2] converting Alternatives to IIfIn 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 conditionals: If x = y Then Else 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, called simply "If", which also eliminates this problem. Its syntax is similar to the IIf function's syntax: IIf in other programming languages
alias testiif { %testiif = 0 echo -a $iif(1,$testiif2,$testiif2) %testiif execution(s) unset %testiif}alias testiif2 { inc %testiif | return testing $!iif: } Calling
var someString := iif(someInt > 35 , 'Large', 'Small'); In this example a new strong type string named "someString" is created (using Type inference) and the 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' References1. ^{{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}} 2. ^{{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}} External links
2 : Microsoft Visual Studio|Conditional constructs |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。