Navigating SAP ABAP Loops: Unraveling 'Continue' and 'Check' Statements, System Variables, and More! (Day 18)

 Welcome back everyone to this Complete SAP ABAP Master Series. We were discussing about loops in our previous blogs, So we will continue it.

Loop Statements :-

  • Exit :- Exit is used to exit from the loop.
  • Continue :- Continue is used to skip the current processing of the record and then process the next record in the loop statements.
  • Check :- If the Check condition is not true, loop will skip the current loop pass and move to next loop pass.

→ We already have seen the practical implementation of exit statement in the previous blog, So let’s continue with Continue statement.


Continue Statement :-

  • Whenever continue statement will trigger, it will skip the current iteration and move to next iteration. let’s see the practical implementation.


    *************************************
    **Using Continue statement in Do loop.
    DATA : lv_input(2) TYPE n VALUE 10.
    
    DO 10 TIMES.
      IF lv_input = 14.
        CONTINUE.
      ENDIF.
      WRITE :/ 'The output is ', lv_input.
      lv_input = lv_input + 1.
    ENDDO.
    
    ************************************
    


    Note :- Our loop is executed 10 times, but as soon as it’s value becomes 14, since the lv_input is not getting increased, we are getting output only till 13.

    Now, see the below example.

    *************************************
    **Using Continue statement in Do loop.
    DATA : lv_input(2) TYPE n VALUE 10.
    
    DO 10 TIMES.
      lv_input = lv_input + 1.
      IF lv_input = 14.
        CONTINUE.
      ENDIF.
      WRITE :/ 'The output is ', lv_input.
    ENDDO.
    
    ************************************
    


    • In the second example, we initialize our local value with 10, and we are increasing its value right in the beginning of the loop and then we are checking for the if condition and then we are using the write statement.
    • So, when the value was 14 it skips the current iteration and rest of the value was printed.

Check Statement :-

  • If the check condition is not true, loop will skip the current loop pass and move to next loop pass.


    **************************************
    ***Using Check Statement in Do loop.
    DATA : lv_input(2) TYPE n VALUE 10.
    
    DO 10 TIMES.
      lv_input = lv_input + 1.
      CHECK lv_input = 15.
      WRITE :/ 'The output is ', lv_input.
    ENDDO.
    
    *************************************
    
    • In this program, It will check for each iteration, if value is 15 then and only then, it allows to execute the below statements, otherwise it will skip the current iteration.



      • Check statements are quite similar to if statements which we have discussed earlier.

System Variables :-

  • System Variables are pre-defined variables in SAP.
  • SYST is the structure for the system fields.
  • All system fields are addressed using SY field name.
  • The various system variables are as follows :-
    • SY-SUBRC :- System variables for return code ( successful = 0, not successful = other than 0).
    • SY-TABIX :- It returns the current line index inside a loop at internal table.
    • SY-INDEX :- It returns the current line index inside do and while loop.
    • SY-DATUM :- System variables for current date(internal format - yyyymmdd).
    • SY-UNAME :- It returns the logon name of the user.
    • SY-UZEIT :- It returns the current system time(internal format - hhmmss).
    • SY-UCOMM :- System variable for user command.

let’s see the practical implementation of the above system variables.

For that create a executable program in ABAP Editor.

SY-UNAME :-

******************************************
*Using SY_UNAME system variable
WRITE : 'The name of the User is' , sy-uname.
******************************************



SY-INDEX

SY-INDEX :- We will use this to get the current index inside do and while loop.

Note :- Never ever use the SY-TABIX inside, the do and while loop to get the current index inside do and while loop, it will never return the current index. It is only used in loop at internal table.

*******************************************
*SY-INDEX
DO 10 TIMES.
  WRITE : / sy-index, sy-tabix.
ENDDO.
********************************************
  • In the above program we have used the SY-INDEX and SY-TABIX both, but in the output you can clearly see SY-TABIX does not work in do and while loop.




    SY-DATUM :-

    • Returns the current date.

      *****************************************
      *SY-DATUM
      WRITE : 'The current date is', SY-DATUM.
      *****************************************
      


      Note :- SAP always stores date in YYYYMMDD format and length is 8.

    Why does the above output is in DD.MM.YYYY format of length 10 ?

    • Since. Date format changes from country to country, So SAP only stores date in YYYYMMDD format and depending upon the user profiles the date is shown to the user.

SY-UZEIT :-

  • It returns the current time.

    *****************************************
    *SY-UZEIT
    WRITE : 'The current time is', SY-UZEIT.
    *****************************************
    


SY-SUBRC :-

  • It is one of the most important system variables and base variable of ABAP programming.
  • RC stands for return code.
  • It returned value is 0, the statement have executed successfully or else the statement has not executed successfully.

So, that’s enough for today.

We will continue with string operations in the next blog.

Thanx alot for being a part of this wonderful journey.

Comments

  1. so CHECK statement can only be used inside the looping statments?

    ReplyDelete

Post a Comment

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming