SAP ABAP — Loops

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.

DO — ENDDO — Unconditional Loop

Looping condition specifies the condition with loop statement (DO/WHILE)

Loop control statement specifies the condition with loop control statement.

DO can be used to execute a certain lines of codes specific number of times.

Example:

REPORT Z_ABAP_DEVELOPER_TEST.

DO 6 TIMES.
WRITE SY-INDEX.
ENDDO.

Output:

Example:

REPORT Z_ABAP_DEVELOPER_TEST.

DATA: G_I TYPE VALUE.
DO 10 TIMES.
Write: / ‘Data Iteration Number ‘, G_I.
G_I = G_I + 1.
ENDDO.

Output:

WHILE ENDWHILE — Conditional Loop

WHILE can be used to execute a certain lines of codes as long as the condition is true.

In while loop, condition is validated before executing the block of statements. If the condition is false for the first time, no statements in the loop gets executed.

The block of statements is either a single statement or multiple statements. The block of statements coded in between WHILE and ENDWHILE statements. ENDWHILE statement is mandatory with WHILE loop.

Example:

DATA: a type i.

a = 0.

WHILE a <> 7.

Write: / ‘This is the line:’, a.
a = a + 1.

ENDWHILE.

Output:

CHECK — Terminate a loop pass conditionally

If the condition is false, the control directly goes to the end statement of the current loop pass ignoring the remaining statements in the current loop pass, starts the next loop pass.

Example:

REPORT Z_ABAP_DEVELOPER_TEST.

DO 6 TIMES.
CHECK sy-index < 4.
WRITE sy-index.
ENDDO.

Output:

--

--

SAP Tutorials - Industrial Engineer & Software Developer & Management İnformation System and Engineering 👩🏻‍💻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Feyza DERİNOĞLU

SAP Tutorials - Industrial Engineer & Software Developer & Management İnformation System and Engineering 👩🏻‍💻