MichaelB.
7/25/2018 - 6:39 PM

Terminate statement block (CHECK, EXIT)

CHECK statement also terminates loop like CONTINUE, but allows you to check certain conditions. Once you define statement and logic, if the condition is not true any remaining statements in the current statement block after the CHECK statement will be ignored and the next loop pass will start. This statement is like a combination of the IF statement and the CONTINUE statement. If condition is untrue, terminate loop. Can check conditions with only one line of code. CHECK statement can be outside of loops, because it also can check other processing blocks such as subroutines or events. Effect depends on location of statement. Outside of processing blocks, it terminates the program.

EXIT lets you terminate loop or statement block without any condition being checked. EXIT terminates entire loop instead of current iteration and executes ENDDO or ENDWHILE. In a nested loop only the loop on that level will terminate, the higher level loop will continue. Can also be used outside of the loop, but will terminate whatever type code structure you're in. Will terminate whole program if it is in calling program and it is not in a statement block.

DATA a TYPE i.

DO 15 TIMES.
  a = a + 1.
  CHECK sy-index <> 2.
  WRITE: / 'Outer Loop cycle: ', a.
ENDDO.

********************************************
* EXIT

EXIT. "exits whole statement block or program


DO 15 TIMES.
  a = a + 1.
  IF sy-index = 3.
    EXIT.
  ENDIF.
  WRITE: / 'Outer Loop cycle: ', a.
ENDDO.
WRITE: / 'Filler'.
WRITE: / 'Filler'.