ABAP Loop Control: The CONTINUE Statement in DO, WHILE and LOOP AT

Category
ABAP-Statements
Published
Author
Johannes

The CONTINUE statement is a control statement that is used exclusively within loop constructs (DO, WHILE, LOOP AT, and the obsolete SELECT...ENDSELECT loop).

Its purpose is to immediately terminate the execution of the current loop iteration and proceed directly to the beginning of the next iteration. All statements that would come after the CONTINUE command in the loop block are skipped for this specific iteration.

Important: CONTINUE does not end the entire loop (that’s what EXIT does), but only ensures that the current iteration is not completed and instead the next one begins (provided the loop condition still allows this).

Syntax

The syntax is very simple:

CONTINUE.

Behavior

When program execution encounters CONTINUE within a loop:

  1. All subsequent statements within the current loop iteration are ignored.
  2. Control jumps directly to the end of the current iteration and initiates the beginning of the next iteration. Specifically this means:
    • For DO ... ENDDO: The loop counter (sy-index) is incremented, and the loop condition (e.g., TIMES) is checked for the next iteration.
    • For WHILE ... ENDWHILE: Control returns to the WHILE <condition> line, and the condition is evaluated again.
    • For LOOP AT ... ENDLOOP: The system attempts to process the next row of the internal table (updates sy-tabix, reads into the work area/field symbol if applicable).
    • For SELECT ... ENDSELECT (obsolete): The next matching database record is fetched.

Context

CONTINUE is only valid and meaningful within the mentioned loop types. Outside a loop, CONTINUE leads to a syntax error.

Distinction from EXIT and CHECK

  • EXIT: Ends the entire loop immediately. Execution continues after the ENDDO/ENDWHILE/ENDLOOP.
  • CHECK <condition>.: Is conditional. Only if <condition> is false, the rest of the current iteration is skipped (then behaves like CONTINUE).
  • CONTINUE: Skips the rest of the current iteration unconditionally when the statement is reached. However, the loop continues in principle.

Examples

1. CONTINUE in LOOP AT (Skipping Certain Rows)

TYPES: BEGIN OF ty_task,
id TYPE i,
description TYPE string,
status TYPE c LENGTH 1, " O=Open, C=Closed
END OF ty_task.
DATA: lt_tasks TYPE STANDARD TABLE OF ty_task,
ls_task TYPE ty_task.
APPEND VALUE #( id = 1 description = 'Task A' status = 'O' ) TO lt_tasks.
APPEND VALUE #( id = 2 description = 'Task B' status = 'C' ) TO lt_tasks.
APPEND VALUE #( id = 3 description = 'Task C' status = 'O' ) TO lt_tasks.
APPEND VALUE #( id = 4 description = 'Task D' status = 'C' ) TO lt_tasks.
WRITE: / 'Processing open tasks:'.
LOOP AT lt_tasks INTO ls_task.
IF ls_task-status = 'C'. " If task is closed...
WRITE: / '-> Task', ls_task-id, 'is closed, being skipped (CONTINUE).'.
CONTINUE. " ... skip the rest and take the next task
ENDIF.
" This code is only executed for open tasks ('O')
WRITE: / '--> Processing task:', ls_task-id, ls_task-description.
" ... actual processing ...
ENDLOOP.

Output:

Processing open tasks:
--> Processing task: 1 Task A
-> Task 2 is closed, being skipped (CONTINUE).
--> Processing task: 3 Task C
-> Task 4 is closed, being skipped (CONTINUE).

2. CONTINUE in DO (Skipping Certain Counter Values)

DO 10 TIMES.
DATA(lv_index) = sy-index.
IF lv_index > 3 AND lv_index < 7. " Skip iterations 4, 5, 6
WRITE: / 'Iteration', lv_index, 'is being skipped.'.
CONTINUE.
ENDIF.
WRITE: / 'Iteration', lv_index, 'is being processed normally.'.
ENDDO.

Output:

Iteration 1 is being processed normally.
Iteration 2 is being processed normally.
Iteration 3 is being processed normally.
Iteration 4 is being skipped.
Iteration 5 is being skipped.
Iteration 6 is being skipped.
Iteration 7 is being processed normally.
Iteration 8 is being processed normally.
Iteration 9 is being processed normally.
Iteration 10 is being processed normally.

3. CONTINUE in WHILE

DATA counter TYPE i VALUE 0.
WHILE counter < 20.
counter = counter + 1.
" Example: Only perform additional action every 5 steps
IF counter MOD 5 <> 0. " If counter is NOT divisible by 5...
CONTINUE. " ... skip the rest of this iteration
ENDIF.
" This action is only executed when counter is 5, 10, 15, 20
WRITE: / 'Additional action at counter:', counter.
ENDWHILE.

Output:

Additional action at counter: 5
Additional action at counter: 10
Additional action at counter: 15
Additional action at counter: 20

Important Notes / Best Practice

  • CONTINUE is a useful tool to make code within loops clearer by handling and skipping certain cases early.
  • It is almost always used within an IF structure to make the skipping dependent on a condition (IF <condition> THEN CONTINUE. ENDIF.).
  • Make sure that using CONTINUE does not impair the readability of the code. Sometimes a restructuring of the IF logic without CONTINUE can be clearer.
  • Ensure that loop variables (like counters in WHILE loops) are also correctly modified when CONTINUE is used to avoid infinite loops.