CASE

CASE 語句比 IF/ELSE-conditional 嚴格得多。它只能比較單個變數和僅相等,而不是大於等於等。

DEFINE VARIABLE c as CHARACTER NO-UNDO。

CASE c:
    WHEN "A" THEN DO:
        RUN procedureA.
    END.
    WHEN "B" THEN DO:
        RUN procedureB.
    END.
    OTHERWISE DO:
        RUN procedureX.
    END.
END CASE.

使用 OR 每個 WHEN 可以比較不同的值:

DEFINE VARIABLE c AS CHARACTER   NO-UNDO.

CASE c:
    WHEN "A" THEN DO:
        RUN procedureA.
    END.
    WHEN "B" OR WHEN "C" THEN DO:
        RUN procedureB-C.
    END.
    OTHERWISE DO:
        RUN procedureX.
    END.
END CASE.

就像 IF 語句一樣,每個分支可以是單個語句或塊。就像 ELSE 宣告一樣,OTHERWISE 不是強制性的。

DEFINE VARIABLE c AS CHARACTER   NO-UNDO.

CASE c:
    WHEN "A" THEN
        RUN procedureA.
    WHEN "B" OR WHEN "C" THEN
        RUN procedureB-C.
END CASE.

與 c 風格的 switch 子句不同,不需要轉義 CASE 語句 - 只執行一個分支。如果幾個 WHEN 只匹配第一個將觸發。OTHERWISE 必須是最後一個,並且只有在上面沒有一個分支匹配時才會觸發。

DEFINE VARIABLE c AS CHARACTER   NO-UNDO.

c = "A".

CASE c:
    WHEN "A" THEN
        MESSAGE "A" VIEW-AS ALERT-BOX. //Only "A" will be messaged
    WHEN "A" OR WHEN "C" THEN
        MESSAGE "A or C" VIEW-AS ALERT-BOX.
END CASE.