You are on page 1of 7

Milan Victor – SAP

MILLU138@GMAIL.COM
ABAP/Webdynpro/CDS/ODATA/SAP UI5/FIORI

Control break statement in SAP ABAP


Purpose of this document:
This document will explain the role and function of the control break statement in SAP ABAP.

What is a Control break statement?


Control Break Statements in SAP ABAP are employed within the AT and ENDAT constructs.
These statements serve to regulate loops in ABAP programming. Executing within a loop, a
Control Break Statement can also be considered a control break event. There is a fundamental
guideline stipulating that control break statements operate exclusively within loops.

It's important to distinguish Control Break Statements from other control statements like
BREAK, EXIT, and CONTINUE. The latter are utilized to terminate and exit the current loop where
they are encountered. In contrast, AT NEW, AT END, AT LAST, AT FIRST are employed to examine
the values of a table field (internal table). When specifically referring to control statements, the
response should encompass BREAK, EXIT, CONTINUE, and CHECK, as they exert control over the
program flow.

Types of control break statement:


 At New
 At First
 At End of
 At Last
 On change of

Prerequisites:
 The key consideration when using the control break statement is that we must sort the
internal table based on the field intended for use in the control break statement.

https://www.linkedin.com/in/milan-victor-xavier-1713b3192
Milan Victor – SAP
MILLU138@GMAIL.COM
ABAP/Webdynpro/CDS/ODATA/SAP UI5/FIORI

 We need to use a temporary work area inside the loop because the control break
statement will set the remaining field value to '***'. Like the below screenshot.

Without Temporary workarea:

The entire work area value becomes '***' except for Material. To address this issue, it is
necessary to use the temporary work area.

With temporary workarea:

Below the screenshot, I have introduced a new workarea as ls_mara_temp. now you can see
the difference

See the difference below screenshots:


LS_MARA_TMP

https://www.linkedin.com/in/milan-victor-xavier-1713b3192
Milan Victor – SAP
MILLU138@GMAIL.COM
ABAP/Webdynpro/CDS/ODATA/SAP UI5/FIORI

LS_MARA

1) AT NEW:
The AT NEW event or control break statement is triggered only when a new record or a specified
field triggers within the loop. For instance, if we aim to display a record only when a new,
repeated "MATNR" is triggered in the loop, we will utilize the AT NEW event or control break
statements in SAP ABAP.
Logic:

Output:

https://www.linkedin.com/in/milan-victor-xavier-1713b3192
Milan Victor – SAP
MILLU138@GMAIL.COM
ABAP/Webdynpro/CDS/ODATA/SAP UI5/FIORI

2) At First:
The AT FIRST event or control break statement is activated solely for the initial record of an
internal table within a loop. For instance, if our objective is to execute an action exclusively
when the first record is triggered, with no action required for subsequent records, we will
employ the AT FIRST event or control break statements in SAP ABAP.

Note: For this no need to specify the field in ‘AT’.


Logic:

Output:

3) At end of:
The "At end of event" or "control break" statement is activated exclusively when a designated
event, such as the conclusion of a record or field, is encountered within a loop. To clarify, if our
objective is to showcase a record only when the final occurrence of the "MATNR" field is
reached in the loop, we utilize "At end of event" or "control break" statements in SAP ABAP.
Logic:

https://www.linkedin.com/in/milan-victor-xavier-1713b3192
Milan Victor – SAP
MILLU138@GMAIL.COM
ABAP/Webdynpro/CDS/ODATA/SAP UI5/FIORI

Output:

4) At Last:
The "At Last event" or "control break" statement is specifically triggered for the concluding
record within an internal table during a loop iteration in SAP ABAP. For instance, if there is a
requirement to execute a particular action solely for the last record and abstain from
performing this action for the preceding records, "At Last event" or "control break" statements
would be employed to achieve this functionality.
Logic:

Output:

5) On change Of:
In SAP ABAP, the "On Change of" control break statement is used to detect a change in the value
of a specified field during the processing of a loop. This statement is particularly useful when
you want to perform certain actions when the value of a specific field changes as you iterate
through the internal table in a loop.

Note: Syntax will be different for this one

https://www.linkedin.com/in/milan-victor-xavier-1713b3192
Milan Victor – SAP
MILLU138@GMAIL.COM
ABAP/Webdynpro/CDS/ODATA/SAP UI5/FIORI

Logic:

Output:

Advantage of Control break statements:


Structured Data Processing:
Control break statements provide a structured way to process data based on specific conditions
or changes in key fields. This helps in organizing and presenting data in a more meaningful way.

Enhanced Readability:
When used appropriately, control break statements can improve the readability of code by
clearly defining sections of logic that apply to different conditions or groups of data.

Efficient Summarization:
Control break statements are often used for summarizing data, such as calculating totals or
averages within specific groups. This can be more efficient than manually iterating through the
data and performing calculations.

Simplified Logic:
For certain scenarios, control break statements can simplify complex conditional logic that
would otherwise require multiple nested IF statements or other constructs.

Grouping and Subtotaling:


They facilitate the grouping of data based on specific criteria and the calculation of subtotals for
each group. This is particularly useful in report generation scenarios.

https://www.linkedin.com/in/milan-victor-xavier-1713b3192
Milan Victor – SAP
MILLU138@GMAIL.COM
ABAP/Webdynpro/CDS/ODATA/SAP UI5/FIORI

Disadvantages of Control Break Statements in SAP ABAP:


Complexity and Maintenance:
While control break statements can simplify logic in some cases, they can also introduce
complexity, making the code harder to understand and maintain, especially as the logic
becomes more intricate.

Performance Overhead:
Depending on the complexity of the control break logic and the size of the data being processed,
there may be a performance impact. Extensive use of control break statements or complex logic
within a loop could lead to slower execution times.

Debugging Challenges:
Debugging code with control break statements might be more challenging than simpler loops.
Identifying the cause of unexpected behavior in a control break scenario may require a thorough
understanding of the logic and the data being processed.

Resource Utilization:
The use of control break statements may require additional resources, especially when dealing
with large datasets. This could result in increased memory consumption or longer processing
times.

Potential for Errors:


Writing complex control break logic increases the likelihood of introducing errors. Small
mistakes in the logic or overlooking certain conditions can lead to incorrect results or
unexpected behavior.

Limited Flexibility:
In some cases, control break logic might not be flexible enough to handle all scenarios. As
requirements change, adapting the existing control break logic may require significant
modifications or restructuring.

Conclusion:
In summary, control break statements in SAP ABAP are powerful tools for organizing and processing
data in loops. They provide a structured way to manage data based on specific conditions or changes in
key fields, offering benefits like improved readability, efficient summarization, and simplified logic.
However, it's important to be aware of potential challenges, such as increased code complexity
impacting maintainability and potential performance considerations with large datasets.
To make the most of control break statements, developers should carefully balance their advantages
and drawbacks. Understanding when and how to use them, along with thoughtful design and
documentation, contributes to creating efficient, readable, and maintainable SAP ABAP code. Using
control break statements appropriately allows developers to streamline data processing and enhance
code clarity, contributing to the development of efficient and readable SAP application.

https://www.linkedin.com/in/milan-victor-xavier-1713b3192

You might also like