Friday, June 27, 2025

πŸ” Loop Control Statements in ABAP: CONTINUE, CHECK, and EXIT

 

SAP ABAP provides specific loop control keywords that help in managing the flow of loop iterations efficiently. These are especially useful when dealing with conditional logic inside DO, WHILE, or LOOP AT statements.


πŸ”„ 1. CONTINUE

πŸ“Œ Description:

The CONTINUE statement skips the current loop iteration and moves to the next iteration of the loop.

πŸ”Ή Syntax:

CONTINUE.

πŸ”Ή Example:

DATA rem TYPE i.
DO 20 TIMES. rem = SY-INDEX MOD 2. IF rem NE 0. CONTINUE. ENDIF. WRITE: / SY-INDEX. ENDDO.

πŸ”Έ Output:

2
4 6 8 ... 20

🧠 Explanation: Only even numbers are printed because CONTINUE skips the iteration if the number is odd.


2. CHECK

πŸ“Œ Description:

The CHECK statement evaluates a condition and:

  • If true, continues with the current loop pass.

  • If false, skips the current iteration.

πŸ”Ή Syntax:

CHECK <expression>.

πŸ”Ή Example:

DATA rem TYPE i.
DO 20 TIMES. rem = SY-INDEX MOD 2. CHECK rem = 2. WRITE: / SY-INDEX. ENDDO.

πŸ”Έ Output:

(No output)

🧠 Explanation: Since rem = 2 is never true (MOD 2 will only result in 0 or 1), the CHECK condition always fails, so no iterations write to the screen.


πŸ›‘ 3. EXIT

πŸ“Œ Description:

The EXIT statement immediately terminates the entire loop, regardless of its iteration state.

πŸ”Ή Syntax:

EXIT.

πŸ”Ή Example:

DATA: n TYPE i VALUE 10. DO n TIMES. IF SY-INDEX >= n. EXIT. ENDIF. WRITE: / SY-INDEX. ENDDO.

πŸ”Έ Output:

1 2 3 4 5 6 7 8 9

🧠 Explanation: Once SY-INDEX reaches the value of n (10), the loop stops executing entirely.

🧾 Summary Table

KeywordBehaviorEffect
CONTINUE    Skips current iteration              Moves to the next loop pass
CHECKEvaluates a condition             Continues if true, skips if false
EXITTerminates entire loop                No further iterations are executed

These keywords are extremely useful for optimizing loop logic, especially in performance-sensitive programs. Use them wisely to make your ABAP code clean and efficient.

No comments:

Post a Comment