The CASE
-statement is a lot more strict than the IF/ELSE
-conditional. It can only compare a single variable and only equality, not larget/smaller than etc.
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.
Using an OR
each WHEN
can compare different values:
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.
Just like with the IF
-statement each branch can either be a single statement or a block. Just like with the ELSE
-statement, OTHERWISE
is not mandatory.
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.
Unlike a c-style switch
-clause there's no need to escape the CASE
-statement - only one branch will be executed. If several WHEN
s match only the first one will trigger. OTHERWISE
must be last and will only trigger if none of the branches above match.
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.