Tag: What are the types of loops in ABAP

  • ABAP LOOP: Everything you need to know

    Preface – This post is part of the ABAP Beginner series.

    ABAP LOOP

    ABAP provides keywords that can be used to run some codes again and again on the basis of a condition called ABAP Loop. Let see them one by one.

    Do-END DO Statements

    These are called unconditional loops because we don’t provide any condition for the loop but provide a fixed number of times for the loop to run. If you want to execute certain codes a fixed number of times, then this is the best loop to use.

     

    WHILE-END WHILE Statements

    These are called conditional loops because we provide conditions for the loop to run. The loop runs and executes the codes till the condition is true.

    In the below figure, we can see the difference between Do and While loop statements.

    abap loop

    FOR LOOP Statements

    For statements are the most used statements in any language. ABAP also provides the same Looping method. Syntax is: FOR i = … [THEN expression] UNTIL|WHILE condition

    You can read more about it here.

    NESTED LOOP

    Nested means using one loop under another loop. It will be clearer with an example.

    Loop Control Statements

    If you want to break the normal flow of an ABAP loop, then you need loop control statements. ABAP provide three-loop control statements, i.e. CONTINUE, CHECK, EXIT.

    CONTINUE

    This statement will pass the current loop unconditionally. It needs IF statements to apply conditions. As soon as this statement is read by the compiler, it skips the current loop and goes to the next iteration. This will be clearer with an example.

    CHECK

    This statement will pass the current loop conditionally. If the CHECK condition is true, then it executes the rest of the statements; otherwise, it skips the current loop and goes to the next iteration. This will be clearer with an example.

    EXIT

    This statement will end all the iterations as soon as it is read by the compiler. It also needs IF statements to apply conditions. After the EXIT statement, the program control goes to the statement just after the LOOP statements.