[WHEN operand1 [OR operand2 [OR operand3 [...]]].
These statements define a control structure that can contain multiple statement blocks
statement_blockn, of which no more than one is executed depending on the value of the operand
Starting with the first
WHEN statement, the content of the operand in
operand is compared with the content of one of the operands
operand2, ... from the top down. The statement block is executed after the
first identical instance is found. If no matches are found, the statement block is executed after the statement
CASEis a general expression position.
operand2, ... after
WHENare extended functional operand positions in which, however, table expressions cannot be specified. This property of operand positions is obsolete and should no longer be used.
If the end of the executed statement block is reached or no statement block is executed, processing continues after
The contents are compared as illustrated in the following logical expression:
operand = operand1 [OR operand = operand2
[OR operand = operand3 [...]]]
For the comparison, the comparison rules for comparisons between any operands apply, depending on the data types of the operands involved.
A statement cannot be placed between the statement
CASEand the first statement
WHEN. In classes, this produces a syntax error; outside classes, obsolete syntax of this type produces a syntax warning.
operand, the current value is used in every comparison. This may differ from the starting value if
operandis a variable that is changed in a functional method specified after a
CASEcontrol structure is somewhat faster than a semantically equivalent
Functional methods and certain predefined functions can be specified after
WHEN, however this should be avoided. Specify constant values, for example, in the operand positions after
The conditional operator
SWITCHcan also be used to make case distinctions in operand positions.
Branches the program flow depending on the function code in system field
CASE sy-ucomm. WHEN 'BACK'. LEAVE TO SCREEN 100. WHEN 'CANCEL'. LEAVE SCREEN. WHEN 'EXIT'. LEAVE PROGRAM. WHEN OTHERS. MESSAGE '...' TYPE 'E'. ENDCASE.
This example demonstrates that the calculation type of an arithmetic expression after
is determined only by its operands. The calculation type after the first
i and the result of the calculation is 0. The comparison with
the value 0 after
WHEN is true regardless of its data type. The calculation
type of the comparison after
however, and the result is false. To force a specific calculation type after
a further operand can be added as shown in the second
CASE statement. This operand is not involved in the calculation.
DATA: inum1 TYPE i VALUE 1, inum2 TYPE i VALUE 3, decf TYPE decfloat34 VALUE 0. CASE inum1 / inum2. WHEN decf. cl_demo_output=>write_text( 'In CASE equal' ). ENDCASE. IF decf <> inum1 / inum2. cl_demo_output=>write_text( 'In IF not equal' ). ENDIF. CASE inum1 / inum2 + decf. WHEN decf. cl_demo_output=>write_text( 'In CASE equal' ). WHEN OTHERS. cl_demo_output=>write_text( 'In CASE not equal' ). ENDCASE. cl_demo_output=>display( ).