Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is IIF() Function 3

Status
Not open for further replies.

Bhashi

Programmer
Sep 13, 2022
15
LK
Can anyone please explain to me what the IIF() function is?
 
Have you tried consulting the Help file? It explains IIF() perfectly clearly, and gives simple example.

That said, I'll add my own explanation:

Here's the syntax:

[tt]IIF(lExpression, eExpression1, eExpression2)[/tt]

The first parameter is a condition, that is, something that evaluates to .T. or .F. If the condition is true, the function returns the second parameter, otherwise it returns the third parameter.

Example:

[tt]PensionAmount = IIF(MyAge > 60, 100, 0)[/tt]

This says that if MyAge > 60, PensionAmount will be 100, otherwise it will be zero.

Does that help at all?

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi

IIF() is the inline, or immediate, IF function. It takes three parameters and returns either the second or third depending on the logical status of the first.

So, using the code below:
Code:
x = IIF(a=b,c,d)

If a=b then IIF will return the value of c and store it in x, if a does not equal b, it will return the value of d and store it in x

It makes for more compact code and may, or may not, execute faster than the long hand version of IF..ELSE..ENDIF
It MIGHT make more compact executables, but not much!



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
That was fast Mike

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
IIF means Immmediate if, it's a shorthand one-liner IF statement with limitations about what can be th code of the IF and else branch.

A further limitation is, that you can't use commands within IIF, you can only have expressions evaluating to a result.

To illustrate an IF statement could do something like

Code:
IF SEEK(value) 
   * record exists, update it
   REPLACE field with something
ELSE
   * record not found, create a new one
   INSERT INTO table (field) Values (something)
ENDIF

And this is not something you can shorten with an IIF.

But an IIF can replace something like that:
Code:
IF SEEK(value) 
   result = field
ELSE
   result = defaultvalue
ENDIF

And you can rewrite that to

Code:
result = IIF(SEEK(value),field,defaultvalue)

And in short the IIF function returns one of two results, which are the second or third parameter of the function, depending on the first parameter being .T. or .F., the first parameter must be a condition, that is an expression that is .T. or .F. - it's the logical condition any normal IF also has at first.

Notice SEEK is a command and I said you can't have commands within IIF, but I use the SEEK() function here. Also notice, an assignment is a command. The reason this works is the IIF part only gives the function result value of either field or defaultvalue and the assignment command is outside of the IIF, using the result of IIF as the value that is assigned, so IIF is possible as the right hand side expression of an assignment. You can't rewrite the long IF version to IIF(SEEK(value), result = field, result = defaultvalue), that doesn't work.

PS: Actually, it does compile, but it will do something you likely don't expect. Because = isn't only the assignment operator, it's also the comparison operator, and so this IIF would check whether SEEK(value) finds the record with the value in the index you seek, and then tell you whether result equals a field, if the record was found or whether result equals the default value, if the record wasn't found. And when result is just a variable not yet set to something else than .F. it could cause a type error. In general, when the field or default value is not the same type as the variable. So, you can also easily fall for some traps, if you aren't aware of the ambiguity that is in languages, also in computer languages.

IIFs become important when you can't use multiline code, for example in SQL you can't embed an IF statement, but you can embed an IIF.

Also, see the help of IIF. Actually, if you come across a command or function or class you don't know, the help always has the topic about it, there's a whole help section about the VFP language, mainly about the two major categories commands and functions and then also all classes and their methods and events. If you still have questions after reading about IIF in the help, then it's fair to ask, but then come with a more concrete question about what you don't understand about the help explanation. I think it's very comprehensively explaining IIF itself, already.

While you're at it, in a late version, I think even just in VFP9, ICASE() was introduced, which extends IIF() to a case with more than two possible results.

Chriss
 
... just to add : very useful in the dynamic methods like dynamicbackcolor, no need for add. functions/methods.
Same is for icase

tom

 
There is a similar shortcut that I often find useful. Whenever the returned value is logical, you can avoid the IIF() completely.

For example, instead of this:

[tt]llResult = IIF(a > b, .T., .F.)[/tt]

you could simply do this:

[tt]llResult = (a > b)[/tt]

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Chris Miller said:
A further limitation is, that you can't use commands within IIF, you can only have expressions evaluating to a result.

With the help of function EXECSCRIPT( ) you should be able to use multiline commands like this:

Code:
a = 0
=IIF( a == 0, EXECSCRIPT( "a = 2" + CHR( 13 ) + "? a" ), EXECSCRIPT( "a = 3" + CHR( 13 ) + "? a" ))
 
While that works, it's much more convoluted and overcomplicated to get to the same result as
Code:
a=0 && or change to anything <>0
a = IIF(a=0,2,3)

I wouldn't use EXECSCRIPT to execute a single command, you could use _vfp.docmd(). Still, there will usually be shorter possibilities and if you want to do something in multiple lines: Put it into a function and call it, you can call functions within expressions, that's quite normal for expressions. And functions can use anything.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top