SAP Database - The Unofficial SAP Knowledge Base

Free Business and Tech Magazines and eBooks

ABAP Programs: Internal flow of control (if, case, do, while)

Print This Post Email This Post Written by admin on Nov 15th, 2007 | Filed under: ABAP Programs

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://abaplearner.blogspot.com

If you like this post, you may as well like these too:

  1. 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...
  2. 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 *...
  3. 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...
  4. 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 [...]...
  5. 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...



This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

  • Subscribe to RSS

  • Free Ebooks