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

Ch3 ProgrammingBasics

This document provides an overview of basic ABAP programming concepts. It discusses arithmetic operators, data types, date/time and currency fields. It covers strings, character manipulation functions, and logical expressions. Control structures like IF/ELSE and CASE are explained. Examples are provided for each topic to demonstrate syntax and usage. The document is intended to cover fundamental programming topics and objectives for a beginner ABAP programming course.

Uploaded by

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

Ch3 ProgrammingBasics

This document provides an overview of basic ABAP programming concepts. It discusses arithmetic operators, data types, date/time and currency fields. It covers strings, character manipulation functions, and logical expressions. Control structures like IF/ELSE and CASE are explained. Examples are provided for each topic to demonstrate syntax and usage. The document is intended to cover fundamental programming topics and objectives for a beginner ABAP programming course.

Uploaded by

Vio Tuns
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

ABAP Programming

Chapter 3. Programming Basics

Objectives

Using arithmetical operators

Performing conversions between different data types

Using Date and time data type

Using Currency data type

Building control routines (IF, CASE, DO, WHILE)

Using System variables

Creating a transaction code

ABAP programming Chapter 3. Programming Basics

Arithmetical Operations
For any arithmetical operation, the calculation itself must appear to the right of the =, and the variable to hold the
result to the left. This ensures that only the result variable will be updated in the execution. The result variables
value will be overwritten with the new value.
Spaces must always be inserted on either side of the operators, including parenthesis. One space is the
minimum, multiple spaces can help lining code up to make it more readable.

Valid arithmetic operators: +, -, *, /, ** (exponentiation), DIV (integer division), MOD.


Assignment syntax. You can use the MOVE statement to transfer the contents of a data object into another data object.
MOVE var2 TO var1.
var1 = var2.

varx = 3 + 7 * 2.
CLEAR varx. The Clear statement resets the contents of a data object to the type-related initial value.
ADD, SUBTRACT, DIVIDE and MULTIPLY statements can be used.
DATA result TYPE p DECIMALS 1 value '-5.5'.
ADD 8 TO result.
WRITE / result.

ABAP programming Chapter 3. Programming Basics

Logical expressions
Logical expressions evaluate to true or false. In ABAP, logical expression cannot be assigned to variables
(no boolean data type). Logical expressions are used in conditional code sequences (IF, CASE).
Operator Operation

=, EQ

Is equal to

<>, NE

Is not equal to

<, LT

Is less than

>, GT

Is greater than

<=, LE

Is less than or equal to

>=, GE

Is greater than or equal to

ABAP programming Chapter 3. Programming Basics

Text Symbols
ABAP is multilingual. This means that when texts are displayed on the user interface, the logon language
of the current user is taken into account. For productive programs that should be executable with different
logon languages, the ABAP programming language provides text symbols.
Text symbols belong to a particular program and can be used in it directly. They are stored outside the
source code in their own Repository object, the text pool for the program. They can be translated into
different languages and each stored with a language indicator in the text pool.

The various texts are placed in the text pool and assigned a 3-character alphanumeric code (xxx) the
ID. This code is then used by specifying either TEXT-xxx instead of the literal. literal(xxx) where literal is
the message in the native language.
For defining text symbols we can either select Goto from the menu bar, Text Elements/Text Symbols or,
address the text symbol in your source code and double-click its ID (forward navigation).
To translate the text symbols of your program choose Goto/ Translation from the ABAP Editor menu.

ABAP programming Chapter 3. Programming Basics

Text Symbols
Defining a text symbol:

Using a text symbol:

MESSAGE text-001 TYPE 'I'.

Translating a text symbol:

ABAP programming Chapter 3. Programming Basics

Strings characters
In SAP, there are two elementary data types used for character strings: data type C and data type N.
Concatenate

REPORT ztest_dt_002bis.
DATA: init_t,
cnp(13) TYPE c,
rezultat(14) TYPE c ,
sep.
init_t = 'T'.
cnp = '2711227365896'.
CONCATENATE init_t cnp INTO rezultat separated by sep.
WRITE rezultat.
ULINE.
Condense

When unwanted spaces appear, the CONDENSE statement can be used. This will remove unnecessary
spaces within a value of a variable.
DATA spaced_name(40) TYPE c VALUE 'Mrs.

Dorina

Birauas'.

CONDENSE spaced_name.
WRITE / spaced_name.

ABAP programming Chapter 3. Programming Basics

Strings characters
Condense No-GAPS

CONDENSE spaced_name NO-GAPS.


Find the length of a string

DATA: spaced_name(40) TYPE c VALUE 'Mrs.


len TYPE i.

Dorina

Birauas',

len = strlen( spaced_name ).


WRITE / len.

Replace
DATA name(40) TYPE c VALUE 'Mrs, Dorina Birauas'.
REPLACE ',' WITH '.' INTO name.
WRITE / name.

ABAP programming Chapter 3. Programming Basics

Strings characters
Search
Two system variables are used with SEARCH: sy-subrc (which identifies if the search was successful or
not, 0 meaning succesfull) and sy-fdpos (if the search is successful is set to the position of the character
string searched for in the variable).
Example 1
DATA name(40) TYPE c VALUE 'Mrs, Dorina Birauas'.
WRITE: / 'name:', name.
SKIP.
SEARCH name FOR ','.
WRITE / 'searching for ","'.
WRITE: / 'sy-subrc:', sy-subrc , / 'sy-fdpos', sy-fdpos.
ULINE.
Example 2

SEARCH name FOR 'B*'.


WRITE / 'searching for "B*"'.
WRITE: / 'sy-subrc:', sy-subrc , / 'sy-fdpos', sy-fdpos.
ULINE.

ABAP programming Chapter 3. Programming Basics

Strings characters
Shift
SHIFT allows moving of the content of a character string left or right, character by character. If there are
no addition, the system will shift just one character to the left.
Example 1
DATA no_ang(10).
no_ang = '0002563'.
SHIFT no_ang LEFT DELETING LEADING '0'.
WRITE: no_ang.

Example 2
no_ang = '0002563'.
WRITE: / no_ang.
SHIFT no_ang CIRCULAR.
WRITE: / no_ang.
If CIRCULAR addition is used, everything will be moved one space to the left, but the character which is
displayed at the beginning of the statement will reappear at the end, rather than leaving a blank space.

ABAP programming Chapter 3. Programming Basics

10

Strings characters
Split
DATA: name(40) TYPE c VALUE 'Mrs. Dorina Birauas',
title(4),
firstname(30),
lastname(30),
sep.
WRITE: 'Name: ', name.
SKIP.
SPLIT name AT sep INTO title firstname lastname.
WRITE: / 'Title:', title.
WRITE: / 'Firstname:', firstname.
WRITE: / 'Lastname:', lastname.

Subfields
DATA: telefon(12) type c,
cod_tara(3) type c,
nrtel(10) type c.
telefon = '+40730012658'.
WRITE: 'Telefon: ', telefon.
SKIP.
cod_tara = telefon(3).
nrtel = telefon+3(9).
WRITE: / 'Cod tara:', cod_tara.
WRITE: / 'Nr. telefon:', nrtel.
ABAP programming Chapter 3. Programming Basics

11

Conversions
The programmer is responsible to ensure that the data types used are compatible with one another
when used for calculations or moving data to and from objects.
SAP has built in automatic data type conversions for many of the standard data types within ABAP,
sometimes the inbuilt conversions do not work.

Conversion rules are predefined logic that determine how the contents of the source field can be entered
into a target field.
Example:
MOVE var2 TO var1.

If both data objects var2 and var1 are of different types, then there is a type conflict. In this case, a type
conversion is carried out automatically, if a conversion rule exists.

ABAP programming Chapter 3. Programming Basics

12

Date and time fields


Date and Time are not stored as numeric data types, but as character data types. Calculations can be
made with them because of the inbuilt automatic data type conversions.
For a date field, the data type is referred to with d, and the limit is 8 characters. The first 4 represent the
year, the next two values the month, and the final two the day. The VALUE addition is used to specify the
date and if it is not used, by default the value is assigned as eight zeros.
DATA: data_a TYPE d VALUE'20140723',
data_b LIKE sy-datum,
data_c TYPE d,
time_a type t VALUE '045400',
time_b LIKE sy-uzeit.
data_b = sy-datum.
time_b = sy-uzeit.
WRITE: / 'Data 1: ', data_a,
/ 'Data 2: ', data_b,
/ 'Data 3: ', data_c.
skip.
WRITE: / 'Ora 1: ', time_a.
WRITE: / 'Ora 2: ', time_b.
Sy-datum and sy-uzeit represent the system date and time.
ABAP programming Chapter 3. Programming Basics

13

Calculations with date and time data type fields


Example
DATA: data_a TYPE sy-datum,
data_b LIKE sy-datum,
data_c type i,
time_a LIKE sy-uzeit,
time_b LIKE sy-uzeit,
time_c type i.

data_a = '19951001'.
data_b = sy-datum.
data_c = data_b - data_a.
time_a = sy-uzeit.
time_b = 080000.
time_c = time_b - time_a.
WRITE: / 'Data angajarii: ', data_a,
/ 'Data curenta: ', data_b,
/ 'Vechimea (in zile): ', data_c.
WRITE: / 'Diferenta in secunde dintre cele 2 ore: ', time_c.
In ABAP, Currency and Quantity are treated the same as packed number fields. Currency must be
declared as data type p, and attention must be paid to the number of decimals. It is better to associate
these fields with the data types of those in a table created in the ABAP Dictionary. This is because the
ABAP dictionary will already have defined the correct field length and number of decimal places for these.
ABAP programming Chapter 3. Programming Basics

14

IF statement
How the programmer structures a program using logical expressions will determine the complete flow of
the program and the sequence in which the actions are performed. IF is a control statement and allows
making decisions, resulting in a number of different outcomes based on the decisions taken.
IF logical_expression.
1 or more statements.

ENDIF.
IF logical_expression.

IF logical_expression.
1 or more statements.
ELSEIF logical_expression.
1 or more statements.
ENDIF.

1 or more statements.
ELSE.

1 or more statements.
ENDIF.

ABAP programming Chapter 3. Programming Basics

15

IF statement
Examples
REPORT ztest_dt_003bis.
DATA: nume(20) TYPE c VALUE 'Pop',
salar_ian(10) TYPE p DECIMALS 2 VALUE 2050,
salar_feb(10) TYPE p DECIMALS 2 VALUE 2023.
PERFORM main.
FORM main .
IF nume = 'Pop'.
WRITE 'Castigator!'.
ENDIF.
SKIP.
IF salar_ian <= salar_feb.
WRITE 'Salar a crescut!'.
ELSE.
WRITE 'Salar a scazut sau a ramas constant'.
ENDIF.
skip.
uline.
IF salar_ian < salar_feb.
WRITE 'Salar a crescut!'.
ELSEIF salar_ian = salar_feb.
WRITE 'Salar a ramas constant!'.
ELSEIF salar_ian > salar_feb.
WRITE 'Salar a scazut!'.
ENDIF.
ENDFORM.
" MAIN
ABAP programming Chapter 3. Programming Basics

16

CASE statement
It is preferable to structure the code across multiple lines to make it more readable and make use of other
control structures if possible. The CASE structure works in a similar way to the IF statement but makes
the code more readable using one logical expression.
CASE variable.
WHEN value1.
1 or more Statements.

WHEN value2.
1 or more Statements.
WHEN OTHERS.

1 or more Statements.
ENDCASE.

REPORT ztest_dt_003bis.
DATA salar_ian(10) TYPE p DECIMALS 2 VALUE 2050.
PERFORM main.

FORM main .
CASE salar_ian.
WHEN 2000.
WRITE 'Cel mai mic salar'.
WHEN 2023.
WRITE 'salar mediu'.
WHEN OTHERS.
WRITE 'cea mai mare valoare a salarului'.
ENDCASE.
ENDFORM.

ABAP programming Chapter 3. Programming Basics

17

Looping
Within loops, sy-index, is a system managed loop counter.
DO value TIMES.
1 or more statements.

ENDDO.
WHILE condition.

1 or more statements.
ENDWHILE.

DO.
1 or more statements.

IF abort_condition.
EXIT.
ENDIF.

ENDDO.
ABAP programming Chapter 3. Programming Basics

18

Looping - Examples
DATA salar_ian(10) TYPE p DECIMALS 2 VALUE 2000.
PERFORM main.
FORM main .
WRITE 'Exemplu Do...Times'.
DO 2 TIMES.
salar_ian = salar_ian + 10.
WRITE: / salar_ian.
ENDDO.
SKIP.
ULINE.
WRITE 'Exemplu While'.
WHILE salar_ian <= 2500.
salar_ian = salar_ian + 100.
WRITE: / 'Salar ', sy-index, ' ' , salar_ian.
ENDWHILE.
SKIP.
ULINE.
WRITE 'Exemplu Do...cu iesire IF'.
DO.
salar_ian = salar_ian + 100.
WRITE: / 'Salar luna ', sy-index, ' ' , salar_ian.
IF salar_ian <= 3000.
EXIT.
ENDIF.
ENDDO.
ENDFORM.

" MAIN
ABAP programming Chapter 3. Programming Basics

19

System fields
System fields provide the application program with information about the actual system status. Several
interesting system fields are presented below:
sy-mandt logon client;

sy-uname logon name of the user;


sy-langu logon language of the user;

sy-datum local date of the ABAP system;


sy-uzeit local time of the ABAP system;
sy-tcode current transaction code;

sy-repid name of the current ABAP program;


sy-index loop counter at DO and WHILE loops;

sy-subrc used with many statements, it is filled by the ABAP runtime system with the
corresponding return code to indicate whether the statement could be executed successfully. The
value zero means that the statement was executed successfully.

ABAP programming Chapter 3. Programming Basics

20

Transaction codes creations


Transactions can be included in a role menu and in a users favorites. In order to start the program, the
transaction code can be entered in the command field.
1. In the Object Navigator, right-click the name of the package, select Create.

ABAP programming Chapter 3. Programming Basics

21

Transaction codes creations


2. Give it a name and enter a short description. Select
Program and selection screen (report transaction).
Select Continue button.
3. Enter the name of the program.
Select SAPGUI for Windows, then click
on the SAVE button from the toolbar.

ABAP programming Chapter 3. Programming Basics

22

Transaction codes creations


4. Insert the name of the package.

5. Click on Save.

ABAP programming Chapter 3. Programming Basics

23

Transaction codes creations


6. Select Create Request, if needed.

7. Insert a short description, then click on Save.

ABAP programming Chapter 3. Programming Basics

24

Transaction codes creations


8. Select the code that is being created (CB1K900295,
in this case).

9. A request is being created. Click on


Continue.

10. The transaction was created.

ABAP programming Chapter 3. Programming Basics

25

Adding Transactions to your personal Favorites


1. In order to add a transaction as a favorite, right click

on Favorites, then Insert transaction.

2. Insert the transaction name, then click on


Continue.

3. The transaction was added.

ABAP programming Chapter 3. Programming Basics

26

Summary
In this chapter we learned how to use arithmetical operators, perform conversions between
different data types, use Date and time data type, Currency data type, build control routines (IF,
CASE, LOOP, DO, WHILE), use System variables and create transaction codes.

ABAP programming Chapter 3. Programming Basics

27

You might also like