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.