The structure of the CASE statement is as follows:
CASE f. WHEN f11 OR f12 ... OR f1n. ... WHEN f21 OR f22 ... OR f2m. ... ... ENDCASE.
On reaching the CASE statement, the processor compares f with
f11 up to f1m. If the result of these comparisons is positive, it
executes the processing block between "WHEN f11 ... f1n." and the
next WHEN statement. If there are no further WHEN statements, it
executes the processing block up to the ENDCASE statement and then
continues with any subsequent processing.
If f is not equal to f11 up to f1n, the processor compares the fields f21 up to f2m in the next WHEN statement with f and proceeds as with f11 and so on.
Although f should be a variable, f11 can be a variable or a literal. For the comparison "f = f11", the rules are the same as for IF.
There is a second variant of the WHEN statement:
No more than one such WHEN statement is allowed within a CASE
block. The "WHEN OTHERS" processing block is always concluded by
ENDCASE, i.e. no further WHEN statements can follow.
The "WHEN OTHERS " processing block is executed only if none of the preceding WHEN blocks have been executed, i.e. if all previous comparisons (" f = ...) have returned a negative result.
DATA: ONE TYPE I VALUE 1, THREE TYPE P VALUE 3. FOUR TYPE P VALUE 4. DO 4 TIMES. CASE SY-INDEX. WHEN ONE. WRITE / 'That is'. WHEN 2. WRITE 'a'. WHEN THREE. WRITE 'good'. WRITE 'example'. WHEN OTHERS. WRITE '!'. ENDCASE. ENDDO.
Output: "That is a good example !"
You can nest several CASE statements and even combine them with IF statements.