Skip to Content

simple way in ABAP to do same as C notation "var = i++"?

Hello experts,

In a loop, I have the following stupid code, I'd like to merge the 2 lines "ADD 1 TO d" and "struc2-fldate = d" (I used a date, but my question is valid for numeric types too) :

DATA struc2 type sflight.
DATA itab2 type table of sflight.
DATA(d) = sy-datum.

SELECT * FROM spfli INTO table @data(itab).

LOOP AT itab INTO data(struc).
  move-corresponding struc to struc2.

ADD 1 TO d. struc2-fldate = d.
APPEND struc2 TO itab2. ENDLOOP.

The only solution I could find is to develop a method LCL_APP=>INC so that to do the following code:

struc2-fldate = lcl_app=>inc( CHANGING counter = d ).

The method is defined as follows:

    CLASS-METHODS inc CHANGING var TYPE any RETURNING VALUE(counter) TYPE decfloat34.

  METHOD inc.
    ADD 1 TO var.
    counter = var.

Is there a simpler way?


Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    Posted on Aug 23, 2016 at 12:10 PM

    The obstacles fort introducing such "a small function" in old ABAP are rather high (name clashes). I asked for those already but without success. 😔

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 23, 2016 at 10:45 AM

    We don't have replacement for i++ in ABAP so your way with the method seems to be only if you'd like to get rid of one line.

    But maybe this is something for Dev team of SAP to add such small but useful function ?

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 23, 2016 at 01:20 PM

    I know using MACRO is kind of old style and should be avoided, but in such cases I wouldn't have problem to use it.

    In my previous company with had a bunch of macros for our framework and actually it made life easier. That could be also an option.


    Add a comment
    10|10000 characters needed characters exceeded

    • Yes, it would be perfect, but for this code, it wouln't be pretty. If ABAP engineers could make a new constructor operator "macro" in next release : struc2-fldate = macro #( inc i ). and DEFINE inc. &1 = &1 + 1. &# = &1. END-OF-DEFINITION, with &# being the auxiliary variable to return. The arguments in macro being the name of the macro followed by the arguments. The compiler would include the macro code before the statement 😊

  • author's profile photo Former Member
    Former Member
    Posted on Aug 23, 2016 at 10:39 AM

    Hi Sandra,

    You can write like this within loop,


    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.