The EXIT statement quits the processing of the current loop. If we use EXIT outside of the loop then it leaves the current processing block. Hence no other statement of that processing block will be executed. It is recommended that EXIT needs to be used inside a loop. We can use it outside of loop as per requirement as well.
If there are nested loops and we use EXIT statement in the inner loop then the system will leave the inner loop after executing the EXIT statement. The program control will continue with the outer loop execution.
REPORT zabap_gui.
* Declaring the local structure of internal table
TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.
* Declaring the Standard internal table with non unique key
DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.
* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
* Now one single row has been fulfilled with data
* Next appending one single row data into the table
APPEND wtab TO itab.
wtab-item = 'Sugar'. wtab-quantity = 1. wtab-price = 90.
APPEND wtab TO itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
APPEND wtab TO itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
APPEND wtab TO itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
APPEND wtab TO itab.
wtab-item = 'Sugar'. wtab-quantity = 2. wtab-price = 70.
APPEND wtab TO itab.
* Sorting the itab by item.
SORT itab BY item.
WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
CLEAR wtab.
ENDLOOP.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
IF wtab-item = 'Rice'.
* Exit always quits the control from the loop iteration
* After exit the loop execution will be finished
EXIT.
ENDIF.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
CLEAR wtab.
ENDLOOP.
SKIP.
WRITE '=========================================='.
* Declaring the local structure of internal table
TYPES:
BEGIN OF ty_tab,
item TYPE char10,
quantity TYPE i,
price TYPE i,
END OF ty_tab.
* Declaring the Standard internal table with non unique key
DATA:
itab TYPE STANDARD TABLE OF ty_tab,
wtab TYPE ty_tab.
* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
* Now one single row has been fulfilled with data
* Next appending one single row data into the table
APPEND wtab TO itab.
wtab-item = 'Sugar'. wtab-quantity = 1. wtab-price = 90.
APPEND wtab TO itab.
wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
APPEND wtab TO itab.
wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150.
APPEND wtab TO itab.
wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
APPEND wtab TO itab.
wtab-item = 'Sugar'. wtab-quantity = 2. wtab-price = 70.
APPEND wtab TO itab.
* Sorting the itab by item.
SORT itab BY item.
WRITE: /3 'Item',
13 'Quantity(KG)',
28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
CLEAR wtab.
ENDLOOP.
WRITE / '=========================================='.
SKIP. " Skipping one single line
LOOP AT itab INTO wtab.
IF wtab-item = 'Rice'.
* Exit always quits the control from the loop iteration
* After exit the loop execution will be finished
EXIT.
ENDIF.
WRITE: /3 wtab-item,
12 wtab-quantity,
25 wtab-price.
CLEAR wtab.
ENDLOOP.
SKIP.
WRITE '=========================================='.
Here the first set of output has come without the exit statement. Second set has come by exit statement. Whenever the program controller finds the item Rice then it executes exit. The system leaves the loop totally. That is why only one output has come yet.