0% found this document useful (0 votes)
32 views

Basic Operation

This document provides an overview of ABAP language syntax and concepts, including: - ABAP is not case sensitive and statements begin with a keyword and end with a period. Chained statements can combine identical start portions. - Comments can be added with asterisks (*) or double quotes (" "). - Variables are declared using the DATA keyword followed by the variable name and type. - Constants are declared with the CONSTANTS keyword and assigned a fixed initial value. - Basic arithmetic operations and assignments can be performed.

Uploaded by

bhuvan9032
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Basic Operation

This document provides an overview of ABAP language syntax and concepts, including: - ABAP is not case sensitive and statements begin with a keyword and end with a period. Chained statements can combine identical start portions. - Comments can be added with asterisks (*) or double quotes (" "). - Variables are declared using the DATA keyword followed by the variable name and type. - Constants are declared with the CONSTANTS keyword and assigned a fixed initial value. - Basic arithmetic operations and assignments can be performed.

Uploaded by

bhuvan9032
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

ABAP language syntax

 ABAP is not case sensitive.


 Every statement begins with a keyword and ends with a period.( WRITE is the keyword
to print on screen )
WRITE 'Hello World!'.
 Chained statements.If consecutive statements have identical part at the beginning, then
ABAP allows you to chain these statements into a single statement. First write the
identical part once and then place a colon (:). Then write the remaining parts of the
individual statements separated by commas.Normal Statements:
 WRITE 'Hello'.
WRITE 'ABAP'.

Chained Statement:

WRITE: 'Hello', 'ABAP'.


 Comments.If you want to make the entire line as comment, then enter asterisk (*) at
the beginning of the line.
* This is a comment line

If you want to make a part of the line as comment, then enter double quote (“) before
the comment.

WRITE 'COMMENT'. "Start of comment

SAP Transaction code is a short cut key attached to a screen. Instead of using SAP easy access menu
we can also navigate to a particular screen in SAP by entering the transaction code (T-code for
short) in the command field of the standard toolbar.

Some of the useful transaction codes for ABAP developers.

T coDe description

SE11 ABAP Data Dictionary

SE16 Data Browser

SE37 Function Builder


SE38 ABAP Editor

SE41 Menu Painter

SE51 Screen Painter

SE71 SAP Script Layout

SE80 ABAP Workbench

SE91 Message Maintenance

SE93 Maintain Transaction

Let us write a “Hello SAP ABAP” program.

Navigate to ABAP editor under Tools node in SAP easy access.


Double click on “ABAP Editor” to open the editor. ABAP editor can also be opened by entering t-
code SE38 in the command field.

This is the ABAP editor’s initial screen. Enter the name of the program you want to create and
press create. All the customer programs must begin with “Y” or “Z”.
In the next popup screen(Program attributes) enter the title for your program, select Executable
program as type and press save.
Press Local Object to store the program in the temporary folder.

This is the screen where you can write the ABAP code.

Write the code. Press save, then syntax check( Ctrl + F2 ).


If there are any syntax errors, it ill be displayed at the bottom of the screen as shown above.
Correct the errors and again check the syntax.

Successful syntax check message will be displayed in the status bar. Then activate( Ctrl + F3 )
the program.
In the following screen select your program and press continue. Then run(F8) the program.

The output will be displayed as shown above.


SAP ABAP Variable

Declaring variables using ABAP data keyword

A variable is a data object whose value can be changed at runtime. To declare a variable,
you use the data keyword with the following syntax:

data variable_name type data_type.

Following the data keyword is the variable name. The variable name must follow the
these rules:

 It cannot exceed 30 characters in length.


 It starts with a letter, and can have Roman letters, digits, and underscores.

After the variable name is the type keyword followed by the data type of the variable.

Variables has two types static variable and reference varaibles.

Static varibles

Static variables have fixed-length types and reserve a data range in memory. For
example:

data gv_name type c(50).

In this example, gv_name is a static variable. It has a data type c whose length is 50.

Note that the variable name starts with gv_ and its description. This is a common
practice to make the code more readable. By convention, g means global, v stands for
the variable. So gv_name indicates that it is a global variable. To declare local variables,
you use lv_ prefix e.g., lv_name.

You can specify an initial value for a static variable by using the optional value keyword
like this:

data gv_name type c(50) value 'John Doe'.

In this example, the gv_name takes the literal characters 'John Doe' as the initial value. If you
do not specify the initial value for gv_name, its defaults value is blank.
ABAP Data Types
Summary: in this tutorial, you will learn about the ABAP data types including
elementary types, complex types, and reference types.

ABAP has three main data types: elementary types. complex types, and reference types.

Elementary types

The elementary type is further divided into the fixed-length type and variable-length
type.

The fixed-length types in ABAP are C, D, N, T, F, I, P, and X. These types consist of:

 Numeric types: I, F, P.
 Character types: C, D, N, T.
 Hexadecimal types: X

When you declare a data object that has a fixed-length type, you must specify its length
in the variable declaration. For example:

data gv_name type c(50).

In this example, the length of the gv_name variable is 50. You cannot store more than 50
characters in the gv_name variable.

The variable-length types are string and xstring. When you declare a variable whose type is
a variable-length type, you don’t need to declare its length. Its length varies during the
execution of the program as you assign values to it. For example:

data gv_job_title type string.

In this example, the gv_job_title has the type of string. Its length will change once you
assign data to it.

gv_job_title = 'SAP Developer'.

In this case, the length of gv_job_title is 13, which is the length of the string 'SAP Developer'.

The following table shows the characteristics of element types:


Data Type Description Initial Size Valid Size Initial Value

C Alphanumeric characters 1 character 1 – 65,535 characters space

N Digits 0-9 1 character 1 – 65,535 characters Character zero

D Date character YYYMMDD 8 characters 8 characters Character zeros

T Time character HHMMSS 8 characters 8 characters Character zeros

X Hexadecimal field 1 byte 1 – 65,535 bytes

P Packed Numbers 8 bytes 1 – 16 bytes 0

I integers range 2^31-1 to 2^31 4 bytes 4 bytes 0

F 8 bytes 8 bytes 0

STRING Like c, but has the variable length

XSTRING like x, but has a variable length

User defined data types


Use TYPES keyword to define the data types.

TYPES: name(10) TYPE c,


length TYPE p DECIMALS 2,
counter TYPE i,
id(5) TYPE n.

Structured data types

Structured data type is grouping of several simple data types under one name.

Use the keywords BEGIN OF and END OF to create a structured data type.

TYPES: BEGIN OF student,


id(5) TYPE n,
name(10) TYPE c,
dob TYPE d,
place(10) TYPE c,
END OF student.

Constants

Constants are used to store a value under a name. We must specify the value when we declare
a constant and the value cannot be changed later in the program.

Use CONSTANTS keyword to declare a constant.

CONSTANTS: pi TYPE p DECIMALS 2 VALUE '3.14',


yes TYPE c VALUE 'X'.

ABAP system variables is accessible from all ABAP programs. These fields are filled by the runtime
environment. The values in these fields indicate the state of the system at any given point of
time.

The complete list of ABAP system variables is found in the SYST table in SAP. Individual fields of
the SYST structure can be accessed either using “SYST-“ or “SY-“.

WRITE:/ 'ABAP System Variables'.


WRITE:/ 'Client : ', sy-mandt.
WRITE:/ 'User : ', sy-uname.
WRITE:/ 'Date : ', sy-datum.
WRITE:/ 'Time : ', sy-uzeit.

Output

Assigning values to ABAP variables


Use ‘=’ or MOVE keyword to assign a value to a variable.

DATA: a TYPE i,
b TYPE i,
c TYPE i,
d TYPE i.

a = 10.
b = a.
MOVE 20 TO c.
MOVE c TO d.

WRITE:/ a, b, c, d.

Output

Basic Arithmetic Operations

DATA: a TYPE i,
b TYPE i,
c TYPE i,
d TYPE i.

*Using Mathematical Expressions


a = 10 + 20.
b = 20 - 10.
c = 10 * 2.
d = 100 / 2.

WRITE:/ 'Using Expressions'.


WRITE:/ a, b, c, d.

*Using Keywords
add 10 to a.
subtract 5 from b.
multiply c by 2.
divide d by 2.

WRITE:/ 'Using Keywords'.


WRITE:/ a, b, c, d.

Output

ABAP constants
Summary: in this tutorial, you will learn how to use the ABAP constants keyword to declare
constants in the program.

Introduction to ABAP constants

Constants are data objects whose values cannot be changed by the program at runtime
using assignments. Their contents are constants.

To declare a constant in the program, you use the constants keyword as follows:

constants constant_name type data_type value intial_value.

In this syntax:

 First, specify the constant name after the constants keyword. The name of a constant has
at most 30 characters and starts with a letter.
 Second, select a data type for the constant after the type keyword.
 Third, specify the value of the constant after the value keyword. This is only place where
you can specify the constant value.

The following example declares a constant named gc_tax_rate with type f and value 0.07.

constants gc_tax_rate type f value '0.07'.

The following shows how to use the constant gc_tax_rate to calculate the net price:

data gv_price type f value 100.


constants gc_tax_rate type f value '0.07'.

data(net_price) = gv_price * ( 1 - gc_tax_rate ).

To declare multiple constants, you use the chain statement as follows:

constants:
constant_name_1 type data_type value constant_value_1,
constant_name_2 type data_type value constant_value_2,
...
constant_name_n type data_type value constant_value_n.
Summary

 Use the keyword constants to declare data object whose values does not change in the
program.

Control flow

ABAP if else
Summary: in this tutorial, you will learn how to use the ABAP if else statement to execute
a block of code based on the result of one or more logical expressions.

The if statement

To execute a block of code based on a logical expression, you use the if statement as
follows:

if logical_expression.
" code block
endif.

In this syntax, the code block inside the if statement will execute if
the logical_expression evaluates to true.

If the logical_expression evaluates to false, the control is passed to the next statement in the


program.

For example:

data gv_status type i value 1.

if gv_status = 1.
write: 'The status is 1'.
endif.
Output:

The status is 1

In this example, the value of the gv_status is 1. The if statement checks if the value


of gv_status is one and outputs a message to the screen.

If you change the value of gv_status to 2, the expression gv_status = 1 evaluates to a false, the
code does not output anything to the screen.

data gv_status type i value 2.

if gv_status = 1.
write: 'The status is 1'.
endif.

To execute another block of code in case the condition in the if statement evaluates false,


you use the if else statement.

The if else statement

The following shows the syntax of the if else statement:

if logical_expression.
" if block
else.
" else block
endif.

In this syntax, if the logical_expression evaluates to true, the code in the if branch will


execute.

In case the logical_expression evaluates to false, the code block in the else branch will execute.

For example:

data gv_status type i value 2.

if gv_status = 1.
write: 'The status is 1'.
else.
write: 'The status is not equal to 1'.
endif.

Output:

The status is not equal to 1


The if elseif else statement

The if elseif else statement allows you to execute a code block based on the results of
multiple boolean expressions:

if logical_expression1.
" if block
elseif logical_expression2.
" elseif block
elseif logical_expression3.
"...
else.
" else block
endif.

In this syntax, ABAP evaluates the logical expressions from top to bottom. If it finds a
logical expression that evaluates to true, it will execute the code block in that branch.

If there is no logical expression that evaluates to true, the code block in the else branch
will execute.

Consider the following example:

data: gv_quantity type i value 120,


gv_price type i.

if gv_quantity > 0 and gv_quantity <= 10.


gv_price = 20.
elseif gv_quantity > 10 and gv_quantity <= 50.
gv_price = 18.
elseif gv_quantity > 50 and gv_quantity <= 100.
gv_price = 16.
else.
gv_price = 15.
endif.

write gv_price.

Output:

15

How it works.

 First, declare two variables gv_quantity and gv_price. The default value of the gv_quantity is


120.
 Second, use the if esleif else statement to determine the value for the gv_price variable
based on the value of the gv_quantity. variable. Since gv_price is 120, the code block in
the else branch is executed, which assigns gv_price the value 15.
 Finally, output the value of the gv_price variable to the screen.

Summary

 Use the if statement when you want to execute a code block based on a logical
expression.
 Use the if else statement in case you want to execute another code block when the logical
expression in the if branch is false.
 Use the if elseif else statement when you want to execute a code block based on the
results of multiple logical expressions.

ABAP case
Summary: in this tutorial, you will learn how to use the ABAP case statement.

Introduction to ABAP case Statement

The case statement defines a control structure that contains multiple code blocks. It


executes no more than one code block by matching an operand with a list of values.
The following shows the syntax of the case statement:

case operand.
when expression_1.
[code_block_1]
when expression_2.
[code_block_2]
...
when expression_n.
[code_block_n]
when others.
[code_block_other]
endcase.

In this syntax, the operand is compared with the expression_1, expression_2, … from the top


down.

If the first match is found, the corresponding code block is executed. In case no match
found, the code_block_other in the when others branch is executed.

The when others branch is optional. If you omit it and there is no match, the processing
continues after endcase.

ABAP case statement examples

The following example illustrates how to use the case statement:

data gv_command type string value 'CANCEL'.


data gv_message type string.

case gv_command.
when 'SAVE'.
gv_message = 'The document has been saved successfully.'.
when 'CANCEL'.
gv_message = 'Are you sure that you want to cancel?'.
endcase.
write gv_message.

How it works.

 First, declare two variables and initialize the gv_command to the value 'CANCEL'.


 Then, assign a message to the gv_message variable by matching the value of
the gv_command with 'SAVE' and 'CANCEL'. Since gv_message value equals the
value 'CANCEL', the gv_message is assigned to 'Are you sure that you want to cancel?'.
 Finally, output the value of the gv_message to the screen.

In this example, we omitted the when others branch.

Summary

 Use the case statement to control which code blocks to execute based on the value of an
expression.

ABAP do
Summary: in this tutorial, you will learn how to use the ABAP do statement to carry an
unconditional loop.

Introduction to the ABAP do loop statement

The do statement allows you to execute a code block in a specified number of times. The
following illustrates the syntax of the do statement:

do [n times]
[code_block]
enddo.

The n times limits the number of iterations in a do loop. The n can be a literal number like
1, 2, 3,… or a numeric expression of type i. If n is less than or equal to 0, the code_block will
not execute at all.

The n times is optional. If you omit it, you need to terminate the loop by the exit statement
in the code_block. Otherwise, the loop executes endlessly until the runtime environment
terminates it.
Within the code_block, you can access the system field sy-index that stores the number of
loops has been passed, which include both previous and the current iterations.

Typically, you use the do statement to fill an internal table with data or to construct
values.

ABAP do example

The following example uses the do loop to calculate and shows the first five square
numbers:

data gv_square type i.

do 5 times.
gv_square = ipow( base = sy-index exp = 2 ).
write: / sy-index, gv_square.
enddo.

Output:

1 1
2 4
3 9
4 16
5 25

How it works.

 The loop executes 5 times.


 In each iteration, use ipow to calculate a square number of the current sy-index and
output the result to the screen.

Summary

 Use the ABAP do loop statement to carry an unconditional loop.

ABAP while
Summary: in this tutorial, you will learn how to use the ABAP while statement to perform
a conditional loop.

Introduction to ABAP while loop

The while loop statement executes a code block as long as a condition is true. The


following shows the syntax of the while statement:

while logical_expression.
[code_block]
endwhile.

In this syntax, the while loop evaluates the logical_expression first. If the result is true, the loop
is entered.

Inside the code_block, you need to change some variables to make the logical_expression false


at some points. Otherwise, you will have an indefinite loop that will execute endlessly
until it is terminated by the runtime environment.

If the logical_expression evaluates to false, the processing continues after the endwhile, no


iteration is executed.

Before each new iteration, the while loop evaluates the logical expression again to
determine whether to execute the next iteration.

Because the while loop evaluates the condition before each iteration, the while loop is
often called a pretest loop.

Similar other loop statements such as do, you can access the system variable sy-index that
stores the current loop index.

ABAP while loop example

The following example shows how to use the ABAP while loop:

data gv_counter type i value 5.

while gv_counter > 0.


write: / 'Counter: ', gv_counter,
'Iteration:', sy-index.
gv_counter = gv_counter - 1.
endwhile.

Output:

Counter: 5 Iteration: 1
Counter: 4 Iteration: 2
Counter: 3 Iteration: 3
Counter: 2 Iteration: 4
Counter: 1 Iteration: 5

How it works.

 First, declare and initialize the gv_counter to 5.


 Then, use the while loop to check if gv_counter is greater than zero and execute the code
that output the gv_counter and the current loop index (sy-index) to the screen. In each
iteration, decrease the gv_counter by 1.

Summary

 Use the while loop statement to execute a code block as long as a condition is true.

ABAP exit
Summary: in this tutorial, you will learn how to terminate a loop immediately using the
ABAP exit staement.

Introduction to ABAP exit statement

Sometimes, you may want to terminate a loop prematurely. To do this, you use
the exit statement in the code block of the loop:

exit.

Typically, you use the exit statement in conjunction with an if statement inside a loop. For
example:

do n times.
" ...
" terminate the loop
if condition.
exit.
endif.
enddo.

Or when you use the exit statement in a while loop:

while condition.
" ...
if condition.
exit.
endif.
endwhile.

If you use the exit statement in a nested loop, it will terminate the innermost loop.

Using exit in a do loop

The following shows how to use the exit statement in a do loop:

do 5 times.
write / sy-index.
if sy-index = 2.
exit.
endif.
enddo.

Output:

1
2

How it works.

 First, execute the code block 5 times using the do statement.


 Second, output the current loop index to the screen. If the current loop index is 2,
terminate the entire loop by using the exit statement.
Using exit in a while loop

The following illustrates how to use exit statement to terminate a while loop:

data gv_counter type i value 1.

while gv_counter > 0.


write / gv_counter.
gv_counter = gv_counter + 1.
if gv_counter = 5.
exit.
endif.
endwhile.
Summary

 Use exit statement to terminate an entire loop prematurely.

ABAP continue
Summary: in this tutorial, you will learn how to use the ABAP continue statement to skip
the current iteration of a loop.

Introduction to ABAP continue statement

The continue statement skips the current iteration of a loop such as a do loop or


a while loop:

continue.

Typically, you use the continue statement in conjunction with an if statement.

The following illustrates how to use the continue statement in a do loop:

do n times.
if condition.
continue.
endif.
" other code
enddo.

In this syntax, the continue statement will start a new loop iteration immediately. It ignores
all the code below the continue between do and enddo.

Similarly, the following shows how to use the continue statement in a while loop:

while condition.
if another_condition.
continue.
endif.
" other code
endwhile.

When you use continue statement in a nested loop, it skips the current iteration of the
innermost loop.

ABAP continue statement examples

The following example uses the continue statement to skip outputting odd numbers to


the screen:

do 10 times.
if sy-index mod 2 = 1.
continue.
endif.
write / sy-index.
enddo.

Output:

2
4
6
8
10
How it works.

 First, execute the code block 10 times using the do statement.


 Second, get the remainder of the division of the current loop index (sy-index) and 2. If the
remainder is 1, skip outputting the current loop index.

Summary

 Use the continue statement to skip the current loop iteration and start a new one.

You might also like