如果...那麼... 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.