SAP Control Break & Loop Control Statements in ABAP

Control break and loop control statements in SAP ABAP help manage data processing efficiently. They simplify internal table handling and improve program readability.

This article covers:

  • Control break statements (AT FIRST, AT LAST, AT NEW, AT END OF)
  • Loop control statements (EXIT, CONTINUE, RETURN)
  • Examples and best practices
SAP Control break statements

SAP Control Break Statements

Control break statements trigger actions at specific points during LOOP AT processing. They help in handling grouped data efficiently.

Types of Control Break Statements

  1. AT FIRST – Executes once before the first record.
  2. AT LAST – Executes once after processing the last record.
  3. AT NEW <Field> – Triggers when a field value changes.
  4. AT END OF <Field> – Executes at the last occurrence of a field value.

Example: Using Control Breaks

abapCopyEditLOOP AT it_data INTO wa_data.
  
  AT FIRST.
    WRITE: / 'Employee Report'.
    WRITE: / '-----------------'.
  ENDAT.

  AT NEW department.
    WRITE: / 'Department:', wa_data-department.
  ENDAT.

  WRITE: / wa_data-emp_id, wa_data-name, wa_data-salary.

  AT END OF department.
    WRITE: / 'Total for', wa_data-department, ':', dept_total.
    dept_total = 0.
  ENDAT.

  AT LAST.
    WRITE: / '-----------------'.
    WRITE: / 'End of Report'.
  ENDAT.

ENDLOOP.

SAP Loop Control Statements

Loop control statements manage the flow within loops. They help in controlling iterations efficiently.

Types of Loop Control Statements

  1. EXIT – Exits the loop immediately.
  2. CONTINUE – Skips the current iteration and moves to the next.
  3. RETURN – Exits the current processing block (method, function, or form).

1. EXIT Statement

EXIT stops the loop execution completely. It is often used in search operations.

Example: Exit Loop on Condition

abapCopyEditLOOP AT it_data INTO wa_data.
  IF wa_data-emp_id = 'E1001'.
    WRITE: / 'Employee found:', wa_data-name.
    EXIT. "Exit loop after finding the employee
  ENDIF.
ENDLOOP.

Use case: Finding the first matching record and stopping further processing.

2. CONTINUE Statement

CONTINUE skips the current iteration and moves to the next record.

Example: Skip Processing for Specific Records

abapCopyEditLOOP AT it_data INTO wa_data.
  IF wa_data-salary < 3000.
    CONTINUE. "Skip employees with salary below 3000
  ENDIF.
  WRITE: / wa_data-emp_id, wa_data-name, wa_data-salary.
ENDLOOP.

Use case: Ignoring certain records based on conditions.

3. RETURN Statement

RETURN exits the current processing block, such as a method or function.

Example: Exit a Method Early

abapCopyEditMETHOD check_employee.
  IF emp_id IS INITIAL.
    RETURN. "Exit method if employee ID is missing
  ENDIF.
  WRITE: / 'Processing Employee:', emp_id.
ENDMETHOD.

Use case: Stopping method execution when a condition is met.

Best Practices for Control Break & Loop Control Statements

  1. Use sorted tables for control break statements to work efficiently.
  2. Minimize logic inside AT blocks to improve performance.
  3. Use EXIT only when necessary to prevent unintended terminations.
  4. Use CONTINUE to skip unnecessary processing and improve efficiency.
  5. Avoid RETURN inside loops unless it’s needed for method exits.

Know more on SAP

1 thought on “SAP Control Break & Loop Control Statements in ABAP”

  1. Pingback: Mastering Core ABAP: 12 Key Concepts for SAP Development

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top