Programming Concepts for Newcomers

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Every program, in any language, is made of the same few ideas: it keeps values in named places, decides between alternatives, repeats work, groups work into reusable pieces, models things as objects and copes with failures. This page introduces those ideas in plain words and shows each one with a tiny ABAP example, so that the rest of this section reads easily even if you have never written code. The examples follow Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608), and everything shown here is valid in both, so it applies to ABAP Cloud development, to classic Standard ABAP on premise and to the Public Edition alike. The examples that use the Expense claims application (a claim has items, and each item has an amount) are meant to be read, not typed in: they use made-up values and need no database.

Before you read

  • No programming experience is needed.

  • To try the examples you will later need either a real SAP system or the local playground described in Hands-on Lab.

  • Getting Started explains where ABAP programs run.

Key terms

  • Variable: a named place in memory that holds one value which the program can change.

  • Data type: the description of what kind of value a variable can hold, such as a whole number or a piece of text.

  • Internal table: a list of rows of the same shape that lives in the program’s memory while it runs.

  • Method: a named, reusable block of code that can receive values, do some work and give a value back.

  • Class: a blueprint that bundles data and the methods that work on it, from which objects are created.

  • Exception: a signal raised when something goes wrong, which the program can catch and handle instead of stopping.

The big picture

A program runs its statements from top to bottom. The ideas below change that straight line: a condition skips some statements, a loop repeats some, a method jumps out to a named block and comes back, and an exception leaves the normal path when something fails.

flowchart TD A[Start: values are stored in variables] --> B{Condition} B -- true --> C[Do one thing] B -- false --> D[Do another thing] C --> E[Loop: repeat for every item] D --> E E --> F[Call a method for reusable work] F --> G{Did something fail?} G -- yes --> H[Catch the exception and handle it] G -- no --> I[Finish] H --> I

Variables and data types

A variable is a named box in memory. Before you use it you tell the system what kind of value it will hold; that kind is its data type. In ABAP the DATA statement declares a variable, TYPE names its type and = stores a value. Common built-in types are i (whole number), string (text of any length), d (a calendar date) and p (a decimal number with a fixed number of decimals, the usual choice for money). Because every variable has a declared type, the system knows how to store, compare and print its value.

The example below declares three variables and prints two of them. It runs in the local playground and printed the two lines shown after the listing.

DATA claim_id TYPE i.
DATA employee TYPE string.
DATA total    TYPE p LENGTH 8 DECIMALS 2.

claim_id = 1001.
employee = `Ada`.
total    = '125.50'.

" An inline declaration creates the variable where it is first used.
DATA(greeting) = |Claim { claim_id } belongs to { employee }|.

WRITE / greeting.
WRITE / total.
Claim 1001 belongs to Ada
125,50

The playground prints the decimal separator as a comma; a real system uses the separator of the user’s settings.

The last line shows two modern conveniences: an inline declaration, which lets the compiler work out the type from the value, and a string template, written between vertical bars, which inserts the values of expressions placed in curly braces. Constants, which never change, are declared with CONSTANTS.

Conditions

A condition lets the program choose. IF runs a block only when a test is true, ELSEIF and ELSE add alternatives, and CASE picks among many fixed values of one variable. Tests use operators such as =, <>, <, > and the words AND, OR and NOT.

The example sorts a claim amount into an approval path and prints it. It runs in the local playground and printed the line shown after the listing.

DATA amount TYPE p LENGTH 8 DECIMALS 2 VALUE '480.00'.
DATA path   TYPE string.

IF amount <= 100.
  path = `automatic`.
ELSEIF amount <= 500.
  path = `manager`.
ELSE.
  path = `finance`.
ENDIF.

DATA status TYPE c LENGTH 1 VALUE 'S'.
CASE status.
  WHEN 'S'.
    path = path && ` (submitted)`.
  WHEN 'A'.
    path = path && ` (approved)`.
  WHEN OTHERS.
    path = path && ` (unknown)`.
ENDCASE.

WRITE / path.
manager (submitted)

Every block is closed by its own keyword (ENDIF, ENDCASE), and every statement ends with a period.

Loops

A loop repeats statements. DO n TIMES repeats a fixed number of times, WHILE repeats as long as a condition is true, and LOOP AT visits every row of an internal table. An internal table is ABAP’s everyday list: a collection of rows of one shape that you fill, search, sort and read in memory. In ABAP a database table and an internal table are different things, and most programs read database rows into an internal table before working on them.

The example adds up the amounts of three claim items and prints the sum and the counter. It runs in the local playground and printed the two lines shown after the listing.

TYPES ty_amounts TYPE STANDARD TABLE OF p WITH EMPTY KEY.
DATA(amounts) = VALUE ty_amounts( ( 20 ) ( 35 ) ( 70 ) ).
DATA sum TYPE p LENGTH 8 DECIMALS 2.

LOOP AT amounts INTO DATA(amount).
  sum = sum + amount.
ENDLOOP.

DATA counter TYPE i.
WHILE counter < 3.
  counter = counter + 1.
ENDWHILE.

WRITE / sum.
WRITE / counter.
125,00
3

Procedures

A procedure is a named block of code you write once and call many times. The modern ABAP procedure is the method, which always belongs to a class. A method declares the values it receives (IMPORTING), the values it hands back (RETURNING) and the work it does. Splitting a program into small methods with clear names keeps each piece easy to read and to test.

The example defines one method that adds value-added tax to an amount. It is a global class, so it requires an SAP system; not run here.

CLASS zcl_tax DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    TYPES ty_money TYPE p LENGTH 8 DECIMALS 2.
    METHODS with_tax
      IMPORTING net          TYPE ty_money
                rate_percent TYPE i
      RETURNING VALUE(gross) TYPE ty_money.
ENDCLASS.

CLASS zcl_tax IMPLEMENTATION.
  METHOD with_tax.
    gross = net + net * rate_percent / 100.
  ENDMETHOD.
ENDCLASS.

Older programs also use subroutines (FORM) and function modules; they still run, and you will meet them in maintenance work. They are covered below under older material.

Objects

An object is a bundle of data and the methods that work on that data. A class is the blueprint, and each object created from it is an instance with its own data. In ABAP you create an object with NEW and call its methods with the arrow ->. A class can keep data private so that only its own methods change it, which prevents other code from putting it into an invalid state.

The example gives a claim object a private total and two methods. It is a global class, so it requires an SAP system; not run here.

CLASS zcl_claim_total DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    TYPES ty_money TYPE p LENGTH 8 DECIMALS 2.
    METHODS add_item IMPORTING amount TYPE ty_money.
    METHODS get_total RETURNING VALUE(total) TYPE ty_money.
  PRIVATE SECTION.
    DATA sum TYPE ty_money.
ENDCLASS.

CLASS zcl_claim_total IMPLEMENTATION.
  METHOD add_item.
    sum = sum + amount.
  ENDMETHOD.
  METHOD get_total.
    total = sum.
  ENDMETHOD.
ENDCLASS.

" Use of the class, for example inside another method:
DATA first  TYPE zcl_claim_total=>ty_money VALUE '20.00'.
DATA second TYPE zcl_claim_total=>ty_money VALUE '35.00'.
DATA(claim) = NEW zcl_claim_total( ).
claim->add_item( first ).
claim->add_item( second ).
DATA(total) = claim->get_total( ).

Classes can also inherit from other classes and implement interfaces; those topics have their own pages later in this section.

Errors and exceptions

Things go wrong: a division by zero, a missing row, a value out of range. An exception is the program’s way of signalling this without carrying on with bad data. The code that might fail sits in a TRY block, and a CATCH block says what to do for a given kind of failure. Catchable exceptions are objects of classes, predefined in the system or written by you, and a program raises one with RAISE EXCEPTION. An exception that nobody catches ends the program with a short dump.

The example catches a division by zero and prints the result. It runs in the local playground and printed the line shown after the listing.

DATA ratio TYPE decfloat34.
DATA divisor TYPE i VALUE 0.

TRY.
    ratio = 100 / divisor.
  CATCH cx_sy_zerodivide.
    ratio = 0.
ENDTRY.

WRITE / ratio.
0

Older code more often reports a failure through a return value that the program has to test itself, instead of through an exception. You will see both styles side by side.

Older material you may meet

Books and code written before ABAP release 7.40 look different from current ABAP, although the old code still runs on Standard ABAP. Variables were declared in a block at the top (DATA: a TYPE i, b TYPE string.) instead of inline with DATA( ... ), text was joined with CONCATENATE instead of string templates, lists were built row by row with APPEND instead of VALUE, and subroutines (PERFORM/FORM) and function modules were the normal way to share code. ABAP for Cloud Development accepts only the language subset and the released APIs of its language version, so some older statements are not available there. When you read an older example, translate it to the modern form before copying the idea.

Where to read more

These pages of the site cover the same ideas in other languages. Comparing them is a good way to see what ABAP shares with the language you may already know:

Summary

  • A variable is a named place that holds a value, and its data type fixes what kind of value that is; DATA declares it.

  • IF, ELSEIF, ELSE and CASE choose between alternatives, and every block ends with its own END... keyword.

  • DO, WHILE and LOOP AT repeat work, and LOOP AT is the usual way to go through an internal table.

  • Methods give reusable work a name; classes bundle data with the methods that use it, and NEW creates an object.

  • TRY and CATCH handle exceptions, while older code more often tests a return value itself.

  • Current ABAP uses inline declarations, string templates and constructor expressions; older code you meet looks different.

Try it

To do this by hand, follow the guide Your First ABAP Program: it writes a class with a run method, runs it and reads the first error message.