SAP ABAP - Case Control Statement


Advertisements

The CASE control statement is used when you need to compare two or more fields.

The syntax for CASE control statement is as follows −

CASE <field>. 
 
WHEN <abc>. 
   <statement block>.
   
WHEN <def>. 
   <tatement block>.
   
WHEN <pqr>. 
   <statement block>. 
...... 
...... 
......  
WHEN <xyz>. 
   <statement block>. 
  
WHEN OTHERS. 
   <statement block>.  
ENDCASE.

The following rules apply to a CASE statement −

  • No logical expressions can be used for the <field> field.

  • The field strings used in the CASE statement are treated as type C variables.

  • The statement block following a WHEN clause is executed if the content of the fields shown in the <field> is similar to one of the fields <abc>, <def>, <ghi> up to <xyz>.

  • After executing all the conditions specified in the WHEN statement, the program continues to process the remaining statements after the ENDCASE statement.

  • The WHEN OTHERS clause is executed in a program when the value of the <field> does not match with any value specified in the <abc> up to <xyz> fields of the WHEN clause.

  • If the WHEN OTHERS clause is omitted and the value of the <field> does not match with any value specified in the <abc> up to <xyz> fields of the WHEN clause, the program continues to process the remaining statements after the ENDCASE statement.

Flow Diagram

Case Control Statement

Example

Report YH_SEP_15.
  
Data: Title_1(10) TYPE C,  
   Title_2(15) TYPE C.  
	
Title_1 = 'ABAP'.
Title_2 = 'Programming'.  

CASE Title_2.
  
WHEN 'ABAP'. 
   Write 'This is not the title'.  
	
WHEN 'Tutorials'.
   Write 'This is not the title'.  
	
WHEN 'Limited'.
   Write 'This is not the title'.
	
WHEN 'Programming'.
   Write 'Yes, this is the title'.
	
WHEN OTHERS.
   Write 'Sorry, Mismatch'.
	 
ENDCASE. 

The above code produces the following output −

Yes, this is the title.
sap_abap_decisions.htm
Advertisements