Code examples · Modern ABAP · Beginner
ABAP first steps: variables, IF, CASE and loops in a runnable class
The problem
A customer order of 1,250.50 EUR gets a discount: 10 percent from 1,000, 5 percent from 500, otherwise none.
How we solve it
- 1Declarevariables
- 2DecideIF and CASE
- 3RepeatDO and WHILE
The result
The console shows the discount calculation, the region and both ways of counting.
1. The requirement
A customer order of 1,250.50 EUR gets a discount: 10 percent from 1,000, 5 percent from 500, otherwise none. The program must also print the sales region for a country code and count from 1 to 3 in two different ways.
2. How we design it
Use a class with the interface IF_OO_ADT_CLASSRUN. That gives one method, MAIN, that you can run with F9 in ADT and that prints to the console. Inside it we do one small thing at a time: declare typed variables, choose the discount with IF, choose the region with CASE, then repeat with DO (fixed number of times) and WHILE (as long as a condition is true).
Why we decided it this way
- 1A class, not an old-style reportClasses are the only way to write ABAP that is also valid in ABAP Cloud. The habits you build now carry over.
- 2Typed variables with decimalsMoney must never be stored in a floating point or integer type. Type P with two decimals keeps cents exact.
- 3IF for ranges, CASE for exact valuesA discount depends on ranges of the amount, so IF with ELSEIF fits. A region depends on exact country codes, so CASE reads more clearly.
Before you start
- ADT (ABAP Development Tools) in Eclipse and a package such as $TMP.
3. The code, step by step
Step 1 of 3: Create the class and declare variables
A class you can run, with typed variables.
" Class definition with the console interface
CLASS zcl_abap_basics DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
" Typed variables
CLASS zcl_abap_basics IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA customer_name TYPE string VALUE 'Acme GmbH'.
DATA amount TYPE p LENGTH 10 DECIMALS 2 VALUE '1250.50'.
DATA discount_pct TYPE i.
DATA discount TYPE p LENGTH 10 DECIMALS 2.
Part by part (plain text)
- Class definition with the console interface. IF_OO_ADT_CLASSRUN adds the method MAIN. When you press F9 in ADT, the system calls MAIN and shows everything you write to OUT in the console.
- Typed variables. DATA creates a variable. STRING holds text of any length. Type P is a packed number: LENGTH 10 DECIMALS 2 keeps two exact decimals, which is right for money. TYPE I is a whole number. VALUE sets the start value.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
The console shows the discount calculation, the region and both ways of counting.
What you should see
Acme GmbH: amount 1250.50, discount 10 % = 125.05 Region: DACH Pass 1 Pass 2 Pass 3 Counter 1 Counter 2 Counter 3
Try it and check the result
- Activate the class with Ctrl+F3 and run it with F9. The console shows the eight lines of the outcome.
- Change the amount to 600 and run again: the discount becomes 5 % and 30.00.
- Change the country to 'IN' and then to 'US': the region line changes to India and Other.
Common mistakes
- Do not store money in type I or F. Use type P with decimals, or a data element that has a currency.
- In WHILE you must change the variable that the condition checks, or the loop never ends.
- A line without a final period is a syntax error: every ABAP statement ends with a period.