Summary -

In this topic, we described about the below sections -

CASE statement used to validate one operand value with multiple values at a time. Each comparision value has its own set of statements to execute.

CASE statement with WHEN OTHERS caluse executes when all other conditions are false. ENDCASE is madatory for every statement. All statements should end with period(.) including CASE, WHEN and ENDCASE.

Syntax -

CASE <Operand>.
	WHEN <Value-1>.
		<Statements-set-1>.
	WHEN <Value-2>.
		<Statements-set-2>.
	WHEN <Value-3>.
		<Statements-set-3>.
	.
	.
	.
	WHEN <Value-n>.
		<Statements-set-n>.
	WHEN OTHERS.
		<Statements-set-others>.
ENDCASE.
<Statements-set-r>.

Variable has the value that needs to be compared with multiple values should code with CASE statement.

WHEN OTHERS clause is optional with CASE statement. But for a better programming practise, it always good to code WHEN OTHER clause in CASE Statement.

Rules -

  • No logical expression or condition used in place of variable.
  • WHEN OTHERS clause is optional and if coded, WHEN OTHERS clause gets executed only when all values are not matched.

Below diagram describes the flow of CASE statement.

Case Statement Flow Diagram

CASE statement coded with variable that needs to be validated with multiple values. WHEN clause always coded with possible matching values for field. Field initially compared with value-1.

If variable value matched with value-1, then statements-set-1 executed. Next, control transfers to statements-set-R. If variable value matched with value-2, then statements-set-2 executed. Next, control transfers to statements-set-R. And so on. If variable value matched with value-n, then statements-set-n executed. Next, control transfers to statements-set-R.

If none of the WHEN clause values are matched, Statements-set-others gets executed as a default that are in WHEN OTHERS clause. Next, control goes to statements-set-R.


Example -

Below example shows how the CASE statement coded in the application program.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_CASE_STATEMENT
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_CASE_STATEMENT.

* Declaring variables
DATA: W_HEIGHT TYPE I VALUE 5.

* Case statement for validating height with different scenarios.
CASE W_HEIGHT.
  WHEN 2.
    WRITE 'The person is 2 feet tall'.
  WHEN 3.
    WRITE 'The person is 3 feet tall'.
  WHEN 4.
    WRITE 'The person is 4 feet tall'.
  WHEN 5.
    WRITE 'The person is 5 feet tall'.
  WHEN 6.
    WRITE 'The person is 6 feet tall'.
  WHEN 7.
    WRITE 'The person is 7 feet tall'.
  WHEN OTHERS.
    WRITE 'The person height is invalid'.
ENDCASE.

Output -

Case Statement Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

CASE W_HEIGHT, validates the W_HEIGHT value with various values coded with WHEN statements. W_HEIGHT is provided as 5, so matches the 'WHEN 5' condition and displays the message 'The person is 5 feet tall'.