Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If, IIf() and If()

I recently asked a question about IIf vs. If and found out that there is another function in VB called If which basically does the same thing as IIf but is a short-circuit.

Does this If function perform better than the IIf function? Does the If statement trump the If and IIf functions?

like image 887
Bryan Roth Avatar asked Aug 26 '08 16:08

Bryan Roth


People also ask

What is the difference between IF and IIF?

Both the IF and IIF first check if the test is true; but the IIF then tests if the value is False. IF doesn't test for the False component – it treats everything not True in the same way. IIF handles those items that aren't True or False (or Unknown) differently to IF.

What is IIF and if in SQL?

The key difference between IF and IIF is that the former is a statement IF test THEN value END IF test THEN value ELSE else END. And the latter is a function IIF(test, then, else, [unknown])

When should I use IIF?

You can use IIf anywhere you can use expressions. You use IIf to determine if another expression is true or false. If the expression is true, IIf returns one value; if it is false, IIf returns another. You specify the values IIf returns.

What does IIF do in tableau?

Tableau IIF Function This function returns BOOLEAN results and categorizes them into 3 categories: TRUE, FALSE, and UNKNOWN. Similar to the IF Statement Tableau, IIF Statement returns a TRUE value when the conditional expression is satisfied, and a FALSE value for a failed condition.


1 Answers

Damn, I really thought you were talking about the operator all along. ;-) Anyway …

Does this If function perform better than the IIf function?

Definitely. Remember, it's built into the language. Only one of the two conditional arguments has to be evaluated, potentially saving a costly operation.

Does the If statement trump the If and IIf functions?

I think you can't compare the two because they do different things. If your code semantically performs an assignment you should emphasize this, instead of the decision-making. Use the If operator here instead of the statement. This is especially true if you can use it in the initialization of a variable because otherwise the variable will be default initialized, resulting in slower code:

Dim result = If(a > 0, Math.Sqrt(a), -1.0)

' versus

Dim result As Double ' Redundant default initialization!
If a > 0 Then
    result = Math.Sqrt(a)
Else
    result = -1
End If
like image 71
Konrad Rudolph Avatar answered Nov 01 '22 14:11

Konrad Rudolph