Вы находитесь на странице: 1из 4

Exercise 13: Basic ABAP Statements

Exercise Objectives
After completing this exercise, you will be able to:

Define elementary data objects


Assign values
Implement conditional branching
Perform calculations

Business Example
You are to create a simple ABAP program for the four basic calculation types. You must be able to
enter the values and the arithmetic operator on a selection screen. Display the result in a list.

Task 1:

Create program

1. Create the executable program ZBC400_##_COMPUTE without a ìTOP includeî.

Task 2:
Define input parameters (implicit definition of the selection screen). 1. Define the input parameters
for two integer values (name suggestion: pa_int1,
pa_int2) and an arithmetic operator (name suggestion: pa_op).

Task 3:

Execute calculations and set up the list output.


1. Additionally, define an elementary data object for the result; type: Packed
number with two decimal places (name suggestion: result).
2. Execute the calculation dependent on the specified arithmetic operator.
Use the CASE statement for a branch.
3. Display the result in a list.

Task 4:
Catch errors.

1. Display an error message on the list if the user has specified an invalid arithmetic operator.
Use the IF statement for checking.

2. Display an error message on the list if the user tries to divide by zero.
Solution 13: Basic ABAP Statements
REPORT sapbc400tss_compute.

PARAMETERS: pa_int1 TYPE i, pa_op(1) TYPE c, pa_int2 TYPE i.

DATA result TYPE p DECIMALS 2.

IF NOT ( pa_op = ’+’ OR 
         pa_op = ’­’ OR 
         pa_op = ’*’ OR 
         pa_op = ’/’ ).

WRITE: ’Invalid operator!’(iop).

ELSEIF pa_op = ’/’ AND pa_int2 = 0.
WRITE: ’No division by zero!’(dbz).
ELSE.

CASE pa_op.
WHEN ’+’.
result = pa_int1 + pa_int2.
WHEN ’­’.
result = pa_int1 ­ pa_int2.
WHEN ’*’.
result = pa_int1 * pa_int2.
WHEN ’/’. 
result = pa_int1 / pa_int2.
ENDCASE.
WRITE: ’Result:’(res), result.
ENDIF.
Exercise 15: Working with Internal Tables
Exercise Objectives
After completing this exercise, you will be able to:

Search for suitable table types in the ABAP Dictionary

Define internal tables based on a global table type

Fill internal tables using array fetch

Process the content of internal tables using a loop

Business Example
You are to output pedido dates stored in database table ekko in a list by using an internal table (as
temporary storage).

Task 1:
Define internal table
1. Create the executable program ZBC400_##_ITAB_LOOP without a TOP include.

2. Buffer the data from the database table ekko in an internal table. You should define an internal
table with a line type that is compatible with the line structure of ekko.

In the ABAP Dictionary, search for all table types that match this condition.

3. Define an internal table (name suggestion: it_ekko) based on one of the found global table
types.
4. Define awork area for the internal table (name suggestion: wa_ekko).

Task 2:
Fill and output the internal table

Program an array fetch access to all data records in the database table ekko as follows:

SELECT * FROM ekko INTO TABLE it_ekko.

Use the LOOP statement to output the buffered data in the internal table in a list.

SOLUCION
DATA it_ekko type standard table of ekko.

DATA wa_spfli LIKE LINE OF it_ekko.

SELECT * FROM ekko INTO TABLE it_ekko.

break wcontreras.

IF sy-subrc = 0.

LOOP AT it_ekko INTO wa_spfli.

WRITE: / wa_spfli-ebeln,

wa_spfli-bedat.

ENDLOOP.

ENDIF.

Вам также может понравиться