如果...那么... ELSE-功能
IF THEN ELSE
也可以像函数一样使用来返回单个值。这很像 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".
使用括号可以简化这样的代码的可读性。
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE c AS CHARACTER NO-UNDO.
c = (IF i < 5 THEN "low" ELSE "high").
IF
-part 的值和 ELSE
-part 的值必须具有相同的数据类型。在这种情况下,不可能使用 ELSE IF
。
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).
可以在 IF
语句中进行几个比较:
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.