ch3_programmingbasics

Upload: vio-tuns

Post on 25-Feb-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Ch3_ProgrammingBasics

    1/27

    ABAP ProgrammingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    2/27

    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 variablesCreating a transaction code

    2ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    3/27

    Arithmetical Operations

    3

    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 resultvariables

    value will be overwritten with the new value.

    Spaces must always be inserted on either side of the operators, including parenthesis. One space is theminimum, 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 programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    4/27

    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

    , GT Is greater than

    =, GE Is greater than or equal to

    4ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    5/27

    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.

    5ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    6/27

    Text Symbols

    6ABAP programmingChapter 3. Programming Basics

    Defining a text symbol:

    Using a text symbol:

    MESSAGE text-001 TYPE 'I'.

    Translating a text symbol:

  • 7/25/2019 Ch3_ProgrammingBasics

    7/27

    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.

    7ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    8/27

    Strings characters

    Condense No-GAPS

    CONDENSE spaced_name NO-GAPS.

    Find the length of a string

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

    len TYPE i.

    len = strlen( spaced_name ).

    WRITE / len.

    Replace

    DATA name(40) TYPE c VALUE 'Mrs, Dorina Birauas'.

    REPLACE ',' WITH '.' INTO name.WRITE / name.

    8ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    9/27

    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.

    9ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    10/27

    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.

    10ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    11/27

    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.

    11ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    12/27

    Conversions

    12ABAP programmingChapter 3. Programming Basics

    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.

  • 7/25/2019 Ch3_ProgrammingBasics

    13/27

    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.

    13ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    14/27

    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.

    14ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    15/27

    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.

    1 or more statements.

    ELSE.

    1 or more statements.

    ENDIF.

    15ABAP programmingChapter 3. Programming Basics

    IF logical_expression.

    1 or more statements.ELSEIF logical_expression.

    1 or more statements.

    ENDIF.

  • 7/25/2019 Ch3_ProgrammingBasics

    16/27

    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 scazut!'.

    ENDIF.

    ENDFORM. " MAIN

    16ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    17/27

    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 makesthe 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.

    17ABAP programmingChapter 3. Programming Basics

    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.

  • 7/25/2019 Ch3_ProgrammingBasics

    18/27

    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.

    18ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    19/27

    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

  • 7/25/2019 Ch3_ProgrammingBasics

    20/27

    System fields

    System fields provide the application program with information about the actual system status. Several

    interesting system fields are presented below:

    sy-mandtlogon client;

    sy-unamelogon name of the user;

    sy-langulogon language of the user;

    sy-datumlocal date of the ABAP system;

    sy-uzeitlocal time of the ABAP system;

    sy-tcodecurrent transaction code;

    sy-repidname of the current ABAP program;

    sy-indexloop 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. Thevalue zero means that the statement was executed successfully.

    20ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    21/27

    Transaction codes creations

    21ABAP programmingChapter 3. Programming Basics

    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.

  • 7/25/2019 Ch3_ProgrammingBasics

    22/27

    Transaction codes creations

    22ABAP programmingChapter 3. Programming Basics

    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.

  • 7/25/2019 Ch3_ProgrammingBasics

    23/27

    Transaction codes creations

    23ABAP programmingChapter 3. Programming Basics

    4. Insert the name of the package.

    5. Click on Save.

  • 7/25/2019 Ch3_ProgrammingBasics

    24/27

    Transaction codes creations

    24ABAP programmingChapter 3. Programming Basics

    6. Select Create Request, if needed.

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

  • 7/25/2019 Ch3_ProgrammingBasics

    25/27

    Transaction codes creations

    25ABAP programmingChapter 3. Programming Basics

    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.

  • 7/25/2019 Ch3_ProgrammingBasics

    26/27

    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.

    Adding Transactions to your personal Favorites

    26ABAP programmingChapter 3. Programming Basics

  • 7/25/2019 Ch3_ProgrammingBasics

    27/27

    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 programmingChapter 3. Programming Basics27