Summary: In this tutorial, you will learn how ABAP statements are written, how spaces and line breaks affect your code, how to add comments, and why uppercase and lowercase letters usually do not change the meaning of ABAP code.
You have already written a small ABAP program:
report zdemo_helloworld.
write 'Hello World'.Now let us slow down and examine the rules that make this code understandable to ABAP. Once these rules feel familiar, longer programs will be much easier to read and write.
What You Will Learn #
By the end of this tutorial, you will be able to:
- Recognize an ABAP statement.
- Identify the keyword at the beginning of a statement.
- End a statement with a period.
- Separate words with spaces.
- Arrange statements over one or more lines.
- Add full-line and end-of-line comments.
- Write ABAP code consistently in lowercase.
- Recognize a simple chained statement without needing to use one yet.
ABAP Statements #
An ABAP program is made up of statements. A statement is an instruction that asks ABAP to do something.
For example:
write 'Hello World'.This statement asks ABAP to display the text Hello World.
A typical statement has three important parts:
keyword + additional information + periodIn the example:
writeis the keyword.'Hello World'is the value that should be displayed..marks the end of the statement.
What Is a Keyword? #
A keyword tells ABAP what kind of instruction you are writing.
You have already used two keywords:
report zdemo_helloworld.
write 'Hello World'.Here:
reportidentifies the executable program.writedisplays something on the output screen.
You will meet many more keywords later. There is no need to memorize them all at once. Learn each keyword when you need it, and pay attention to the pattern of the complete statement.
End Every Statement with a Period #
Every ABAP statement ends with a period.
Correct:
write 'Hello World'.Incorrect:
write 'Hello World'The period tells ABAP that the instruction is complete. If you forget it, ABAP may treat the next line as part of the same unfinished statement.
Consider this code:
write 'Hello'.
write 'ABAP'.There are two statements because there are two terminating periods.
A useful habit is to check the end of each statement whenever the editor reports a syntax problem.
Separate Words with Spaces #
Words in an ABAP statement must be separated by at least one space.
Correct:
write 'Hello World'.Incorrect:
write'Hello World'.The space after write separates the keyword from the text that follows it.
You may use more than one space:
write 'Hello World'.ABAP can read this statement, but unnecessary spacing makes code harder to scan. Prefer a single space unless extra alignment genuinely improves readability.
Using Line Breaks #
ABAP uses the period, not the end of a line, to determine where a statement finishes. This means a statement can occupy one line or several lines.
This statement is written on one line:
write 'Hello World'.The same statement can be spread over several lines:
write
'Hello World'
.Both versions are valid, but the first is easier to read because the statement is short.
You can also place more than one statement on one line:
write 'Hello'. write 'World'.This is valid, but it is not a good style for normal source code. Put separate statements on separate lines so that learners, teammates, and your future self can follow the program more easily:
write 'Hello'.
write 'World'.Valid code is not always readable code. Choose the layout that makes the program easiest to understand.
ABAP Comments #
A comment is a note written for people who read the source code. ABAP ignores the comment when it prepares and runs the program.
Comments are useful when they explain why the code exists or clarify something that is not immediately obvious.
ABAP offers two simple ways to write comments.
Commenting an Entire Line with an Asterisk #
Place an asterisk * in the first position of a line to make the entire line a comment:
* Display a greeting for the learner
write 'Hello World'.The asterisk must be the first character on that line. Do not place spaces before it.
Correct:
* This entire line is a commentDo not rely on this form with leading spaces:
* This asterisk is not in the first positionCommenting the Rest of a Line with a Double Quotation Mark #
A double quotation mark " tells ABAP to ignore the rest of the line:
write 'Hello World'. " Display a greetingThe statement before the double quotation mark still runs. Only the text after it is treated as a comment.
You can also use a double quotation mark at the beginning of an otherwise empty line:
" Display a greeting for the learner
write 'Hello World'.This style is convenient because the comment can be indented with the surrounding code.
Write Helpful Comments #
A useful comment adds information that the code does not already communicate clearly.
This comment merely repeats the statement:
* Write Hello World
write 'Hello World'.This comment explains the purpose more clearly:
* Confirm that the first program runs successfully
write 'Hello World'.For a tiny example, a comment may not be necessary. As programs become larger, thoughtful comments can help readers understand important decisions.
Uppercase and Lowercase Letters #
ABAP keywords and program names are generally not case-sensitive. Therefore, these statements have the same meaning:
write 'Hello'.
WRITE 'Hello'.
Write 'Hello'.Even though all three forms work, mixing styles makes code look untidy. This tutorial uses lowercase consistently:
report zdemo_syntax.
write 'Hello'.
write 'ABAP'.Notice that the text inside quotation marks keeps the letters exactly as you write them:
write 'Hello'.
write 'HELLO'.These statements display different text:
HelloHELLOSo remember the practical distinction:
- The spelling case of keywords such as
writedoes not change the instruction. - The spelling case of text inside quotation marks changes the displayed text.
A First Look at Chained Statements #
You may encounter a colon and commas in existing ABAP code. They can combine consecutive statements that share the same beginning.
These two statements are easy to read:
write 'Hi'.
write 'How are you?'.They can also be written as one chained statement:
write: 'Hi',
'How are you?'.Read the chained form as though write appeared before each item:
write 'Hi'.
write 'How are you?'.In the chained statement:
- The colon
:follows the shared beginning. - A comma
,separates the remaining parts. - One period
.ends the complete chain.
For now, prefer the two separate write statements. They are clearer for a beginner, and they make each instruction easy to inspect. Chained statements are more useful in certain declarations that you will learn later.
Do not try to chain every pair of similar statements. A shorter statement is only better when it remains easy to understand.
Common Mistakes #
Forgetting the Final Period #
Incorrect:
write 'Welcome'Correct:
write 'Welcome'.Removing the Space after a Keyword #
Incorrect:
write'Welcome'.Correct:
write 'Welcome'.Placing an Asterisk after Leading Spaces #
An asterisk creates a full-line comment only when it is in the first position:
* A full-line commentIf you want an indented comment, use a double quotation mark:
" An indented commentForgetting That Quoted Text Keeps Its Case #
These keywords mean the same thing:
write 'Hello'.
WRITE 'Hello'.These text values do not look the same in the output:
write 'Hello'.
write 'HELLO'.Putting Too Much Code on One Line #
This is valid but difficult to scan:
write 'One'. write 'Two'. write 'Three'.Prefer:
write 'One'.
write 'Two'.
write 'Three'.Exercise: Make the Program Easy to Read #
Create an executable program named zdemo_syntax and enter this code:
report zdemo_syntax.
* Display the beginning of the message
write 'ABAP syntax'.
" Complete the message
write ' is easy to read'.Tasks #
- Activate and run the program.
- Confirm that comments do not appear in the output.
- Change both
writekeywords to uppercase and run the program again. - Confirm that changing the keyword case does not change the result.
- Change the code back to lowercase for a consistent style.
- Remove one period, activate the program, and read the error message.
- Restore the period and activate the program successfully.
Expected Output #
ABAP syntax is easy to readChallenge #
Rewrite the two write statements as a chained statement. Activate and run the program, then decide which version is easier for you to read.
Summary #
In this tutorial, you learned that:
- An ABAP program consists of statements.
- A statement normally begins with a keyword and ends with a period.
- Words in a statement are separated by spaces.
- A statement can span several lines because the period marks its end.
- Separate statements should normally be placed on separate lines for readability.
- An asterisk in the first position comments out an entire line.
- A double quotation mark comments out the rest of a line.
- ABAP keywords and program names are generally not case-sensitive.
- Text inside quotation marks keeps the uppercase and lowercase letters you enter.
- A chained statement uses a colon, commas, and one final period, but separate operational statements are often easier to read.
Thank you for your feedback!