ABAP does not have a ++ operator like C or Java do. The best equivalent for ++ is the += operator starting in Netweaver 7.54. On older systems you can use the add command.
Let us first have a look at how exactly the increment operator works in C or Java. Then we can translate it correctly into ABAP.
What does the ++ operator mean in C or Java?
If you want to translate C code containing the increment operatotor ++ into ABAP, you first need to understand exactly what this operator means.
The increment operator in Java comes in two variations: pre-increment and post-increment. So
a = i++
means: take the current value of i, copy it to a and after that increment i by 1.
a = ++i
on the other hand means: first take the value of i, increment it by one and and after that copy it to a.
There are two ways to remember which one is which:
The code is always executed from left to right. So if ++ comes before the variable, then ++ is executed first, before the assignment.
The other way is to have a look at the old joke about the C++ programming language.
new_language = C++;
This means: The new language does, what C does. Then increment the name only.
What was intended with the new language should have been called ++C.
The direct translation to ABAP
Given that ABAP does not have a ++ operator we have to do the increment manually:
" instead of a = i++ use a = i. i = i + 1. " instead of a = ++i use i = i + 1. a = i. " instead of a = i-- use a = i. i = i - 1. " instead of a = --i use i = i - 1. a = i.
This does the trick. Always. However when the variable name is long like in
myreallylongvariable = myreallylongvariable + 1
then the readability of your code suffers. In these cases there are two things that you can do:
The clumsy keyword approach “add 1 to”
There is an old syntax that ABAP inherited from COBOL
add 1 to myreallylongvariable.
This at least means that the reader of your code does not have to check that the left hand and the right hand operands are the same and when the variable name is long, then this syntax is easier to read indee.
The new += operator
Real relief comes with NetWeaver 7.54 which allows
a += 1. a -= 1. a *= 5. a /= 2.
On systems older than 7.54 you still have to use one of the other approaches.
What ABAP still cannot do
What ABAP still cannot do, even in 7.54, is to press increment/decrement and assignment to another variable into one line. So you still cannot do this Java code in ABAP
int a=5,i; i=++a + ++a + a++; i=a++ + ++a + ++a; a=++a + ++a + a++; System.out.println(a); System.out.println(i);
Well, read it twice or try it out and you will understand that this feature of Java is one that you do not really need in ABAP.
The new += operator should be sufficient.
If your ABAP system is older than 7.54 then you might consider an upgrade. Newer ABAP releases have a number of features that can make your code several lines shorter and that still are better to read. You should discuss that with your client. S/4HANA is at the doorstep anyway. So make sure to keep your system up to date.
You can find more articles about SAP here.