4.2    ABAP Syntax

The source code of an ABAP program is simply a collection of various ABAP statements, interpreted by the runtime environment to perform specific tasks. You use declarative statements to define data objects, modularization statements to define processing blocks, and database statements to work with the data in the database.

In this section, we’ll look at the basic syntax rules that every ABAP programmer should know. We’ll then look at the use of chained statements and comment lines.

4.2.1    Basic Syntax Rules

There are certain basic syntax rules that need to be followed while writing ABAP statements:

In Figure 4.2, the program shown consists of three ABAP statements written across three lines. The first word in each of these statements (REPORT, PARAMETERS, and WRITE) is a keyword. As you can see, each statement begins with a keyword and ends with a period. Also, each ABAP word is separated by a space.

ABAP Statement

Figure 4.2    ABAP Statement

You can write multiple statements on one line or one statement can extend over multiple lines. Therefore, if you wish, you can rewrite the code in Figure 4.2 as shown:

REPORT ZCA_DEMO_PROGRAM. PARAMETERS p_input(10) TYPE c. WRITE p_input RIGHT-JUSTIFIED.

However, to keep the code legible, we recommend restricting your program to one statement per line. In some cases, it’s recommended to break a single statement across multiple lines—for example:

SELECT * FROM mara INTO TABLE it_mara WHERE matnr EQ p_matnr.

The above statement may be written as shown in Listing 4.1 to make it more legible.

SELECT * FROM mara 
INTO TABLE it_mara
WHERE matnr EQ p_matnr.

Listing 4.1    Example of Splitting Statement across Multiple Lines

4.2.2    Chained Statements

If more than one statement starts with the same keyword, you can use a colon (:) as a chain operator and separate each statement with a comma. These are called chained statements, and they help you avoid repeating the same keyword on each line.

For example,

DATA v_name(20) TYPE c.
DATA v_age TYPE i.

Also can be written as

DATA : v_name(20) TYPE c,
v_age TYPE i.

End the last statement in the chain with a period. Chained statements are not limited to keywords; you can put any identical first part of a chain of statements before the colon and write the remaining parts of the individual statements separated by comma—for example,

v_total = v_total + 1. 
v_total = v_total + 2.
v_total = v_total + 3.
v_total = v_total + 4.

can be chained as

v_total = v_total + : 1, 2, 3, 4.  
Note

ABAP code is not case-sensitive, so you can use either uppercase or lowercase to write ABAP statements. We recommend writing keywords and their additions in uppercase and using lowercase for other words in the statement to make the code more legible.

4.2.3    Comment Lines

To make your source code easy to understand for other programmers, you can add comments to it (see Listing 4.2). Comment lines are ignored by the system when the program is generated, and they’re useful in many ways.

DATA f1 TYPE c LENGTH 2 VALUE 'T3'.
DATA f2 TYPE n LENGTH 2.
*This is a comment line
f2 = f1.
WRITE f2. "This is also a comment line

Listing 4.2    Comment lines

There are two ways to add comment lines in source code:

You can comment (i.e., set as a comment) on a block of lines at once (a multiline comment) by selecting the lines to be commented on and pressing (Ctrl) + (<) on the keyboard. Similarly, to uncomment (i.e., set as normal code) a block of lines, you can select the lines and press (Ctrl) + (>).

Alternatively, you can also use the context menu to comment or uncomment code. To comment a line of code or a block lines, select the code, right-click, and select the appropriate option from the Format context menu. This helps you avoid the tedious job of adding asterisks manually at the beginning of each line.

Now that you have a better understanding of basic ABAP syntax rules and chaining ABAP statements, in the next section, we’ll look at the keywords used in ABAP.