ABAP Control Structures
Control structures in ABAP allow for conditional execution and looping, enabling the creation of complex logic within programs. This guide provides an in-depth overview of the control structures available in ABAP, including conditional statements, loops, and case structures.
Conditional Statements
IF...ENDIF
-
Basic Syntax:
IF condition. " code to execute if condition is true ENDIF.
-
With ELSE:
IF condition. " code to execute if condition is true ELSE. " code to execute if condition is false ENDIF.
-
With ELSEIF:
IF condition1. " code to execute if condition1 is true ELSEIF condition2. " code to execute if condition2 is true ELSE. " code to execute if both conditions are false ENDIF.
-
Nested IF Statements:
IF condition1. IF condition2. " code to execute if both conditions are true ENDIF. ENDIF.
CASE...ENDCASE
-
Basic Syntax:
CASE variable. WHEN value1. " code to execute if variable equals value1 WHEN value2. " code to execute if variable equals value2 WHEN OTHERS. " code to execute if variable does not match any previous value ENDCASE.
-
Example:
DATA: lv_color TYPE char10. CASE lv_color. WHEN 'RED'. WRITE: / 'Stop'. WHEN 'GREEN'. WRITE: / 'Go'. WHEN 'YELLOW'. WRITE: / 'Caution'. WHEN OTHERS. WRITE: / 'Invalid color'. ENDCASE.
Looping Structures
DO...ENDDO
-
Fixed-Number Loop:
DO 5 TIMES. " code to execute 5 times ENDDO.
-
Infinite Loop (with EXIT):
DO. " code to execute indefinitely IF condition. EXIT. ENDIF. ENDDO.
WHILE...ENDWHILE
-
Basic Syntax:
WHILE condition. " code to execute while condition is true ENDWHILE.
-
Example:
DATA: lv_count TYPE I VALUE 1. WHILE lv_count <= 10. WRITE: / lv_count. lv_count = lv_count + 1. ENDWHILE.
LOOP AT...ENDLOOP
-
Looping Through Internal Tables:
LOOP AT it_table INTO wa_table. " code to execute for each row in the internal table ENDLOOP.
-
Example:
DATA: it_students TYPE TABLE OF string, wa_student TYPE string. LOOP AT it_students INTO wa_student. WRITE: / wa_student. ENDLOOP.
Other Control Flow Commands
CHECK
-
Usage:
CHECK condition.
- If the condition is false, the current iteration of a loop or processing block is terminated, and control passes to the next iteration or the next statement after the loop.
-
Example:
DATA: lv_num TYPE I. DO 10 TIMES. lv_num = lv_num + 1. CHECK lv_num MOD 2 = 0. WRITE: / lv_num. ENDDO.
EXIT
-
Usage:
EXIT.
- Exits the current loop or processing block immediately.
-
Example:
DO 10 TIMES. IF lv_num = 5. EXIT. ENDIF. WRITE: / lv_num. ENDDO.
CONTINUE
-
Usage:
CONTINUE.
- Skips the remaining statements in the current loop iteration and proceeds with the next iteration.
-
Example:
DO 10 TIMES. lv_num = lv_num + 1. IF lv_num MOD 2 <> 0. CONTINUE. ENDIF. WRITE: / lv_num. ENDDO.
RETURN
-
Usage:
RETURN.
- Exits the current subroutine, function module, or method.
-
Example:
FORM example_form. IF condition = 'exit'. RETURN. ENDIF. WRITE: / 'Condition not met'. ENDFORM.
Example Program with Control Structures
REPORT z_control_structures_example.
DATA: lv_num TYPE I VALUE 1,
lv_sum TYPE I VALUE 0.
WHILE lv_num <= 10.
IF lv_num MOD 2 = 0.
WRITE: / 'Even number:', lv_num.
ELSE.
WRITE: / 'Odd number:', lv_num.
ENDIF.
lv_sum = lv_sum + lv_num.
IF lv_sum > 20.
EXIT.
ENDIF.
lv_num = lv_num + 1.
ENDWHILE.
WRITE: / 'Sum:', lv_sum.
Conclusion
This guide provides a comprehensive overview of the control structures in ABAP, which are essential for creating logical and efficient programs. These structures allow for the manipulation of program flow, enabling complex decision-making and iteration over data sets. For more advanced usage and specific scenarios, consult the SAP documentation or additional resources.