IF THEN ELSE
can also be used like a function to return a single value. This is a lot like the ternary ?
-operator of C.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE c AS CHARACTER NO-UNDO.
/* Set c to "low" if i is less than 5 otherwise set it to "high"
c = IF i < 5 THEN "low" ELSE "high".
Using parenthesis can ease readability for code like this.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE c AS CHARACTER NO-UNDO.
c = (IF i < 5 THEN "low" ELSE "high").
The value of the IF
-part and the value of the ELSE
-part must be of the same datatype. It's not possible to use ELSE IF
in this case.
DEFINE VARIABLE dat AS DATE NO-UNDO.
DEFINE VARIABLE beforeTheFifth AS LOGICAL NO-UNDO.
dat = TODAY.
beforeTheFifth = (IF DAY(dat) < 5 THEN TRUE ELSE FALSE).
Several comparisons can be done in the IF
-statement:
DEFINE VARIABLE between5and10 AS LOGICAL NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO INIT 7.
between5and10 = (IF i >= 5 AND i <= 10 THEN TRUE ELSE FALSE).
MESSAGE between5and10 VIEW-AS ALERT-BOX.