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