If
This is the original branching command. It lets you choose between two paths of action based on a condition. If the condition is true, you follow the first path. If it's not true, you take the second path. IF can only be used in programmatic code. To have conditional code in an expression, use IIF().
Usage |
IF lCondition [ THEN ] Commands [ ELSE Commands ] ENDIF |
Example |
* move forward, but don't end up at EOF SKIP IF EOF() GO BOTTOM ENDIF * print one message for kids - another for adults IF YEAR(DATE()) - YEAR(dBirthdate) < 21 ? "Come see our incredibly cool new gizmos" ELSE ? "Come in now for amazing bargains" ENDIF |
SELECT Transactions IF NOT EOF() AND Transactions.lPrint * Print out this record ENDIFIn this example, we count on the NOT EOF() test being passed before we check the field lPrint. Now the down side:
IF lCondition AND MyUDF() * do something ENDIFIn this example, we have to keep in mind that MyUDF() is called only when lCondition is true, so we shouldn't be counting on something to happen in MyUDF() every time we execute the IF. Similarly, when testing your code, make sure you test all combinations of conditions. If you've tested your code when the first few conditions always cause a branch, you may not detect problems at the end of your test:
IF MyUDF() OR HerUDF() OR TheirUDF() OR Syntax Error * do something ENDIFThe code above will work fine until the first three functions each return .F.IFs can be nested one inside another. If you find yourself nesting a whole series of Ifs, though, take a look at DO CASE—it may be more appropriate for the situation.
See Also |