BC400 Lesson: Working with EIementary Data Objects
Exercise 5: Basic ABAP Statements
Exercise Objectives AIter completing this exercise, you will be able to: DeIine elementary data objects Assign values Implement conditional branching PerIorm calculations Business ExampIe You are to create a simple ABAP program Ior the Iour 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: DeIine input parameters (implicit deIinition oI the selection screen). 1. DeIine the input parameters Ior two integer values (name suggestion: paint1, paint2) and an arithmetic operator (name suggestion: paop). Task 3: Execute calculations and set up the list output. 1. Additionally, deIine an elementary data object Ior the result; type: Packed number with two decimal places (name suggestion: result). 2. Execute the calculation dependent on the speciIied arithmetic operator. Use the CASE statement Ior a branch. 3. Display the result in a list. Task 4: Catch errors. 1. Display an error message on the list iI the user has speciIied an invalid arithmetic operator. Continuea on next page 2006/Q2 2006 SAP AG. All rights reserved. 99
Unit 4: Basic ABAP Language EIements BC400 Use the IF statement Ior checking. 2. Display an error message on the list iI the user tries to divide by zero. Hint: In a later lesson you will learn how to implement the catching oI these errors with a more 'elegant user dialog. 100 2006 SAP AG. All rights reserved. 2006/Q2
BC400 Lesson: Working with EIementary Data Objects SoIution 5: Basic ABAP Statements Task 1: Create program 1. Create the executable program ZBC400_##_COMPUTE without a 'TOP include. a) Carry out this step as usual. Task 2: DeIine input parameters (implicit deIinition oI the selection screen). 1. DeIine the input parameters Ior two integer values (name suggestion: paint1, paint2) and an arithmetic operator (name suggestion: paop). a) See source code excerpt in the model solution. Task 3: Execute calculations and set up the list output. 1. Additionally, deIine an elementary data object Ior the result; type: Packed number with two decimal places (name suggestion: result). a) See source code excerpt in the model solution. 2. Execute the calculation dependent on the speciIied arithmetic operator. Use the CASE statement Ior a branch. a) See source code excerpt in the model solution. 3. Display the result in a list. a) See source code excerpt in the model solution. Task 4: Catch errors. 1. Display an error message on the list iI the user has speciIied an invalid arithmetic operator. Use the IF statement Ior checking. a) See source code excerpt in the model solution. Continuea on next page 2006/Q2 2006 SAP AG. All rights reserved. 101
Unit 4: Basic ABAP Language EIements BC400 2. Display an error message on the list iI the user tries to divide by zero. Hint: In a later lesson you will learn how to implement the catching oI these errors with a more 'elegant user dialog. a) See source code excerpt in the model solution. ResuIt Source text section: SAPBC400TSS_COMPUTE 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. Continuea on next page 102 2006 SAP AG. All rights reserved. 2006/Q2
BC400 Lesson: Working with EIementary Data Objects WRITE: 'Result:'(res), result. ENDIF. Note: You will learn about the additions in parentheses Ior the WRITE statement in a later lesson. You can omit them here. 2006/Q2 2006 SAP AG. All rights reserved. 103