ABAP syntax


This brief description of the ABAP syntax begins inevitably with the ubiquitous "Hello World" program.

"Helloworld"

PROGRAM TEST.
WRITE 'Hello World'.

This example contains two statements, one on each line. The keywords are PROGRAM and WRITE. The program displays a list on the screen. In this case, the list consists of the line "Hello World".

Formatting rules

White space significance

ABAP has no format restrictions. You can enter statements in any format, so a statement can be indented, you can write several statements on one line, or spread a single statement over several lines. The only requirement is that every statement ends in a period.

You must separate words within a statement with at least one space. The system also interprets the end of line marker as a space.

The two-line "Hello World" program from above could also be written as

PROGRAM TEST. WRITE 'Hello World' .

or even as:

               PROGRAM 
               TEST.
                          WRITE
                          'Hello World'.

Free formatting is convenient, but with complex code, such as deeply nested IF/ELSE blocks, it can get tricky. The ABAP editor therefore offers a "Pretty Printer" function, which can take care of proper indentation.

One obvious exception to the free-formatting rule are text literals. A text literal is a sequence of alphanumeric characters in the program code enclosed in single quotes. If a text literal in an ABAP statement extends across more than one line, then a ‘&’ character must be used to combine a succession of text literals into a single one. Example:

USERPROMPT = 'Please double-click on a line in the output list ' &
             'to see the complete details of the transaction.'.

Case sensitivity

ABAP statements are not case-sensitive. The following code is perfectly permissible:

proGRAm TEsT.
WriTe 'Hello World'.

Users can configure the way source text is presented (all upper case, all lower case, ABAP keywords in upper case and variable names in lower case, etc.) according to their own preference. Typically the keywords are in upper case and everything else (variable, function, method, and class names) in lower case.

Chained statements

The ABAP programming language allows you to concatenate consecutive statements with an identical first part into a chain statement.

To concatenate a sequence of separate statements, write the identical part only once and place a colon (:) after it. After the colon, write the remaining parts of the individual statements, separating them with commas. Ensure that you place a period (.) after the last part to inform the system where the chain ends.

Chaining is very often used in WRITE statements. WRITE accepts just one argument, so if for instance you wanted to display three fields from a structure called FLIGHTINFO, you would have to code:

WRITE FLIGHTINFO-CITYFROM.
WRITE FLIGHTINFO-CITYTO.
WRITE FLIGHTINFO-AIRPTO.

Chaining the statements results in a more readable and more intuitive form:

WRITE: FLIGHTINFO-CITYFROM, FLIGHTINFO-CITYTO, FLIGHTINFO-AIRPTO.

In the chain, a colon separates the beginning of the statement from the variable parts. After the colon or commas, you can insert any number of spaces.

You could, for example, write the same statement like this:

WRITE:    FLIGHTINFO-CITYFROM,
          FLIGHTINFO-CITYTO,
          FLIGHTINFO-AIRPTO.

In a chain statement, the first part (before the colon) is not limited to the keyword of the statements. For example, the code

SUM = SUM + 1.
SUM = SUM + 2.
SUM = SUM + 3.
SUM = SUM + 4.

could be written in chained form:

SUM = SUM + : 1, 2, 3, 4.

Comments

ABAP has 2 ways of defining text as a comment.

An asterisk (*) in the leftmost column of a line makes that line a comment. A double quotation mark («"») anywhere on a line makes the rest of that line a comment.

Example

***************************************
** Program: BOOKINGS                 **
** Author: Joe Byte, 07-Jul-2007     **
***************************************
 
REPORT BOOKINGS.
 
* Read flight bookings from the database
SELECT * FROM FLIGHTINFO
  WHERE CLASS = 'Y'       "Y = economy
  OR    CLASS = 'C'.      "C = business
(...)