IF, CHECK & WHEN ABAP commends example source code and information

The IF and the CHECK statements perform similar functionality but whereas The IF statement gives you the option to run different code based on if the condition is true or not, the CHECK just stops the current unit of processing code (i.e. LOOP, PERFORM, FM etc) if the condition is false. See example ABAP code below:

data: it_ekko type STANDARD TABLE OF ekko,
      wa_ekko like line of it_ekko.
SELECT *
  from ekko
  into table it_ekko
 where ebeln eq '1111'.
if sy-subrc = 0.
* code for if sy-subrc = 0
elseif sy-subrc = 4.
* code for if sy-subrc = 4
else.
* code for if sy-subrc not = 0 or 4
endif.
SELECT *
  from ekko
  into table it_ekko
 where ebeln eq '1111'.
check sy-subrc = 0.
*only progresses further of sy-subrc = 0

The check statement kind of acts like a combination of the IF & EXIT commands. So when you want to stop processing within something like a loop, perform or function module etc. Instead of using the IF/EXIT commands you can just use the CHECK.

SELECT *
  from ekko
  into table it_ekko.
loop at it_ekko into wa_ekko.
  if sy-tabix eq 14.
    EXIT.
  endif.
*At line 14 the loop processing would stop
endloop.
SELECT *
  from ekko
  into table it_ekko.
loop at it_ekko into wa_ekko.
  check sy-tabix lt 14.
*At line 14 the loop processing would stop
endloop.

In conclusion there isn't really anything the check can do that you can't do with the IF and EXIT commands, just provides a slightly neater way of doing a true/fasle check using a few less lines of ABAP code. The IF on the other hand allows for further conditions other than true or false via the ELSEIF and ELSE statements.


WHEN statement
It's probably at this point I should mention the CASE, WHEN statements, which can be used in the same way as the IF. i.e.

CASE sy-subrc.
  When 0.
*            code for if sy-subrc = 0
  When 4.
*           code for if sy-subrc = 4
  When others.
*           code for if sy-subrc not = 0 or 4
endifcase.

Like the CHECK there is nothing really a when can do that can't be achieved by an IF ELSE but does look a bit neater and is potential more efficient. You can also do something like this if you want to find out which flag has been checked (i.e. contains the value X). For some reason it looks cleverer than it actually is, just seems to switch around what you would expect in the When and case sections.

CASE 'X'.
  When ld_flag1.
  When ld_flag2.
  When ld_flag3.
ENDCASE.

Again the IF does have a further trick up its sleeve that makes it the most comprehensive of the options discussed here and that is the ability to use operators such as AND, OR, NOT etc. for example you could have something like

SELECT *
  from ekko
  into table it_ekko
 where ebeln eq '1111'.
if sy-subrc = 0 or sy-subrc = 4.
* code for if sy-subrc = 0 or 4
elseif sy-subrc = 8 and
       sy-datum gt '20120101' .
* code for if sy-subrc = 8 and date is greater than 01.01.2012
else.
* code for if sy-subrc not = 0 or 4
endif.

Related Articles

ABAP COLLECT statement syntax to add up all numeric internal table values within SAP
ABAP DELETE statement keyword to delete data from SAP internal and database tables
ABAP DESCRIBE statement keyword to get information about tables and fields
PERFORM TABLES command passing internal table as parameter
ABAP read command to read line of internal table in SAP
ABAP UPDATE command to modify database field values
AUTHORITY-CHECK abap Statement / command
ABAP delete command in SAP
ABAP MODIFY statement to update SAP data within database and internal tables
ABAP WRITE statement command with in SAP to display data report to users
SAP ABAP Statement syntax including basic implementation code
Concatenate ABAP statement syntax for concatenating data objects, sting values or rows of an SAP internal table
ABAP EXPORT data TO MEMORY ID and import it back again
Call Function module IN BACKGROUND TASK - Execute abap code in seperate background process
Function module IN UPDATE TASK statement - Execute abap code in seperate unit of work
ABAP STRLEN command to get the value length of a SAP field
SAP ABAP SELECT command and its different uses
SELECT..ENDSELECT command
ABAP FOR ALL ENTRIES SELECT statement addition in SAP data retrieval
ABAP SELECT inner join statement to select from two tables at the same time
SELECT directly into an internal table
SELECT directly into an internal table when field order is different
Function module STARTING NEW TASK statement - Execute abap code in seperate work process