In This Topic
Selection Structures
These let a formula return different results depending on a value, a test or a comparison
IF function
Returns a value if a test condition evaluates to TRUE and another value if it evaluates to FALSE.
Syntax:
IF ( logical_test1, stat_list1, [ logical_test2, stat_list2, ]... stat_list_else )
IFF ( logical_test1, stat_list1, [ logical_test2, stat_list2, ]... stat_list_else )
- logical_testN is any value or comparison or expression that evaluates to TRUE or FALSE.
- stat_listN is the statement list (or single statement) that is evaluated if logical_testN is TRUE.
- stat_list_else is the statement list (or single statement) that is evaluated if none of the logical_testN values is TRUE.
Example:
IF (x < 0, "negative", x > 0, "positive", "zero")
IF THEN ELSE
Evaluate one statement list if a test condition evaluates to TRUE and another statement list if it evaluates to FALSE.
Syntax:
IF logical_test1 THEN stat_list1 [ ELSE IF logical_test2 THEN statlist2 ]... [ ELSE stat_list_else ] [ END IF ]
IF logical_test1 THEN BEGIN stat_list1 END [ ELSE IF logical_test2 THEN BEGIN statlist2 END ]... [ ELSE BEGIN stat_list_else END]
IF ( logical_test1 ) { stat_list1 } [ ELSE IF ( logical_test2 ) { statlist2 } ]... [ ELSE { stat_list_else } ]
Example:
IF mark > 50 THEN "pass" ELSE "fail" ENDIF
- returns "pass" or "fail" depending on the value of 'mark'
SELECT
Evaluates the statement associated with a value, depending on which case matches the test value. This differs from the IF THEN ELSE type selection because it is specifically designed for multiple cases, not just true and false.
Syntax:
SELECT [CASE] test
CASE value1 [ : ] stat_list1
CASE from2 TO upto2 [ : ] stat_list2
CASE IS op value3 [ : ] stat_list3
CASE ELSE stat_list_else
END SELECT
CASE test OF
value1 : stat_list1
from2 TO upto2 : stat_list2
IS op value3 : stat_list3
ELSE stat_list_else
END
SWITCH ( test ) {
CASE value1 : stat_list1
CASE from2 TO upto2 : stat_list2
CASE IS op value3 : stat_list3
DEFAULT : stat_list_else
}
- test is evaluated to give a value which is used to select the required case.
- value1 is a value that the test value must be equal to for the case to be selected.
- from2 TO upto2 is the range of values that the test value must lie within for the case to be selected.
- op is the comparison operator ( = <> < >= > <= ) to use to compare the test value with value3.
- value3 is the value to compare with the test value for that case to be selected.
- stat_listX is a statement list that is evaluated when the corresponding case is selected.
- stat_list_else is a statement list that is evaluated when none of the case values equals the value of the expression.
Even though the [ : ] is optional, it is recommended that you use it to avoid ambiguity.
e.g. "CASE 1 -1" would be an error because it would treat "1 -1" as an expression that evaluates to zero. Instead use "CASE 1 : -1"
You can combine multiple cases using commas, and the case is selected is any of them match
The CASE ELSE, ELSE, or DEFAULT must be the last case
You can use "ENDSELECT" instead of "END SELECT"
Example:
SELECT count
CASE 0 : "None"
CASE 1 : "One"
CASE 2 : "A couple"
CASE 3, 4, 5 : "A few"
CASE 10 TO 20 : "Many"
CASE IS > 20 : "A lot"
CASE ELSE "Several"
END SELECT
TRY CATCH
Evaluate statements, and if there is an error evaluate the catch statement.
Syntax:
TRY statlist CATCH statlist_catch [ END TRY | END CATCH ]
- statlist is the statements to evaluate.
- statlist_catch is the statements to evalaute if there is any error in statlist
NOTE: You can use "ELSEIF" instead of "ELSE IF"
NOTE: You can use "ENDIF" instead of "END IF"
Example:
IF mark > 50 THEN "pass" ELSE "fail" ENDIF
- returns "pass" or "fail" depending on the value of 'mark'