sapdev logo background
sapdev logo sapdev logo
Comments

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.




comments powered by Disqus