ABAP Programs: Internal flow of control (if, case, do, while)
REPORT ZSOURCE0902.
* Declarations for later use
TABLES CUSTOMERS.
DATA: COLOR(10) VALUE ‘yellow’,
N(4) TYPE N VALUE ’123′,
P TYPE P,
C4(4) VALUE ’124′,
C5(5) VALUE ’00124′,
SQUARE_NUMBER TYPE I,
X TYPE I,
Y TYPE I.
* Using a condition (e.g., business class or not)
IF CUSTOMERS-CUSTTYPE = ‘B’.
* book business class
WRITE ‘B’.
ELSE.
* book economy class
WRITE ‘Something else’.
ENDIF.
* Nested if clauses
IF N > 0.
N = N + 1.
ELSE.
IF N = 0.
WRITE / ‘zero’.
ELSE.
N = N – 1.
ENDIF.
ENDIF.
* Using elseif instead of a nested if clauses
IF N > 0.
N = N + 1.
ELSEIF N = 0.
WRITE / ‘zero’.
ELSE.
N = N – 1.
ENDIF.
* Using a case clause
CASE COLOR.
WHEN ‘red’. WRITE ‘color is red’.
WHEN ‘green’. WRITE ‘color is green’.
WHEN ‘yellow’. WRITE ‘color is yellow’.
WHEN OTHERS. WRITE ‘non-standard color’.
ENDCASE.
* Some logical expressions in if clauses
IF N IS INITIAL.
WRITE ‘initial’.
ELSEIF N LT 0
OR N GT 5.
WRITE / ‘less than zero or greater than 5′.
ELSE.
WRITE / ‘something else’.
ENDIF.
IF N > P.
WRITE / ‘n is greater than p’.
ENDIF.
* Conversion in an expression
IF C4 = C5.
WRITE / ‘c4 and c5 are equal’.
ENDIF.
* Comparing character strings
DATA: A(6) VALUE ‘ABAP/4′,
RESULT(6).
IF A CA ‘XP’.
RESULT = A+SY-FDPOS(2).
WRITE / RESULT.
ENDIF.
IF A CO ‘ABP’.
WRITE / ‘a only contains A,B, and P’.
ENDIF.
IF A CS ‘BAP’.
WRITE / ‘a contains the string BAP’.
ENDIF.
IF A CP ‘*AP++’.
WRITE / ‘a contains AP followed by two more characters’.
ENDIF.
* Unconditional loop
DO 100 TIMES.
SQUARE_NUMBER = SY-INDEX ** 2.
WRITE / SQUARE_NUMBER.
ENDDO.
* Terminating a loop
DO.
* terminate loop after 5 steps or when the color is red
IF SY-INDEX > 5 OR COLOR = ‘red’. EXIT. ENDIF.
* main loop step
WRITE / SY-INDEX.
ENDDO.
* Using a conditional loop.
X = Y – 2.
WHILE X <> Y.
X = Y + 1.
WRITE / X.
IF X > Y. EXIT. ENDIF.
ENDWHILE.
———————
ABAPer, mail: abap.community@gmail.com http://www.erpdb.info
If you like this post, you may as well like these too:
- ABAP Programs: External flow of control (events) REPORT ZSOURCE0901. * Display a list of customersTABLES CUSTOMERS.SELECT * FROM CUSTOMERS. WRITE / CUSTOMERS-NAME.ENDSELECT. * Event for drill downAT LINE-SELECTION. WRITE: / ‘This line appears after drill-down’. ~~~~~ end...
- ABAP Programs: Records and internal tables REPORT ZSOURCE0407. * Records (or structures) consist of a fixed number of componentsDATA: BEGIN OF CUSTOMER, ID(8) TYPE N, NAME(25), TELEPHONE(12), END OF CUSTOMER. * Working with the different components...
- ABAP Programs: A simple internal table REPORT ZSOURCE1201.* Work area for a database tableTABLES CUSTOMERS.* Defining an internal tableDATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100.* Reading all entries of the database table into the internal tableSELECT *...
- ABAP Programs: Internal tables with header lines REPORT ZSOURCE1202.* Work area for a database tableTABLES CUSTOMERS.* Defining an internal table with header lineDATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100 WITH HEADER LINE.* Reading all entries of the database...
- ABAP Programs: Upload Excel to Internal table Here is a simple program which will upload the excel file to the internal table using the function module ‘TEXT_CONVERT_XLS_TO_SAP’. REPORT Z_EXCEL_2_ITAB. ************************************** * http://www.erpdb.info * ************************************** TYPE-POOLS: truxs. PARAMETERS:...
- ABAP Programs: Using Internal Tables for Selection Criteria REPORT ZSOURCE1112.* Work areasTABLES: CUSTOMERS, BOOKINGS.* Internal tablesDATA: ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100 WITH HEADER LINE, ALL_BOOKINGS LIKE BOOKINGS OCCURS 500 WITH HEADER LINE.* Reading entries from both database tableSELECT...
- CASE: ABAP Keyword a day CASE Basic formCASE f. Effect Case distinction.Depending on the current contents of a field, this statement executes one of several alternative processing branches. The field whose contents determine how the...
- ABAP Programs: Using internal tables as snapshots of database tables REPORT ZSOURCE1103.* Work area for a database tableTABLES CUSTOMERS.* Internal tableDATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100 WITH HEADER LINE.* Filling the internal table with all entries of the database tableSELECT...
- ABAP Programs: Filling an internal table from a database table REPORT ZSOURCE1203.* Work area for a database tableTABLES CUSTOMERS.* Defining an internal table with header lineDATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100 WITH HEADER LINE.* Filling the internal table (previous content...
- ABAP Tips on Data Dictionary and Internal tables Data Dictionary Maintenance Views Lock Objects Internal Tables Free text search in a field in a internal table Types of Internal Tables Working with Internal Tables...
- ABAP: How to create a dynamic internal table and work area FIELD-SYMBOLS:<fs_table> TYPE STANDARD TABLE, <fs_table_wa> TYPE ANY. DATA: table_ref TYPE REF TO data, wa_ref TYPE REF TO data. DATA: l_structure TYPE dd02l-tabname value 'VBAP'. CREATE DATA table_ref TYPE STANDARD...
- Splitter control and Graphs in ABAP View/Download the Source Code for Splitter Control and Graph...
- ABAP Source Code: Syntax of ABAP/4 Programs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 * Declaration...
- ABAP Programs: Importing from the ABAP/4 Memory REPORT ZSOURCE1402. * Internal tables which will be imported DATA: ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100 WITH HEADER LINE, ALL_BOOKINGS LIKE BOOKINGS OCCURS 10 WITH HEADER LINE, NEW_BOOKINGS LIKE BOOKINGS OCCURS...
- ABAP Programs: Exporting to the ABAP/4 Memory REPORT ZSOURCE1401. * Work areas TABLES: CUSTOMERS, BOOKINGS. * Internal tables which will be exported DATA: ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100 WITH HEADER LINE, ALL_BOOKINGS LIKE BOOKINGS OCCURS 10 WITH...



















Leave a Reply