Module 4 - Control Structures
Control structures define the flow of execution in an ABAP program. They decide when, how often, and under what condition a block of code runs. Mastering control structures is essential for writing correct, readable, and maintainable ABAP programs.
1. Conditional Statements
1.1 IF / ELSEIF / ELSE
The IF statement executes code when a logical condition is true.
IF condition.
" statements
ELSEIF condition.
" statements
ELSE.
" statements
ENDIF.
Example
DATA(lv_marks) = 72.
IF lv_marks >= 75.
WRITE 'Distinction'.
ELSEIF lv_marks >= 60.
WRITE 'First Class'.
ELSE.
WRITE 'Pass'.
ENDIF.
Use IF when conditions involve ranges, logical operators, or multiple variables.
1.2 CASE Statement
The CASE statement is used when multiple conditions depend on a single variable.
CASE lv_grade.
WHEN 'A'.
WRITE 'Excellent'.
WHEN 'B'.
WRITE 'Good'.
WHEN 'C'.
WRITE 'Average'.
WHEN OTHERS.
WRITE 'Invalid grade'.
ENDCASE.
Prefer CASE over multiple IF...ELSEIF statements for better readability and performance.
2. Looping Constructs
2.1 DO Loop
The DO loop executes a fixed number of times or indefinitely.
DO 5 TIMES.
WRITE sy-index.
ENDDO.
Infinite Loop Example
DO.
IF sy-index > 10.
EXIT.
ENDIF.
ENDDO.
Always ensure an EXIT condition in infinite loops.
2.2 WHILE Loop
The WHILE loop executes as long as a condition remains true.
DATA(lv_counter) = 1.
WHILE lv_counter <= 5.
WRITE lv_counter.
lv_counter += 1.
ENDWHILE.
Use WHILE when the number of iterations is not known in advance.
2.3 LOOP AT (Internal Tables)
The LOOP AT statement iterates over internal tables.
DATA lt_numbers TYPE STANDARD TABLE OF i.
lt_numbers = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).
LOOP AT lt_numbers INTO DATA(lv_num).
WRITE lv_num.
ENDLOOP.
Use inline declarations inside loops for cleaner code.
3. Loop Control Statements
3.1 CONTINUE
CONTINUE skips the current loop iteration and proceeds to the next one.
LOOP AT lt_numbers INTO DATA(lv_num).
IF lv_num = 2.
CONTINUE.
ENDIF.
WRITE lv_num.
ENDLOOP.
3.2 EXIT
EXIT terminates the loop completely.
LOOP AT lt_numbers INTO DATA(lv_num).
IF lv_num = 2.
EXIT.
ENDIF.
WRITE lv_num.
ENDLOOP.
Use EXIT to leave a loop. Use RETURN to exit a method or function.
3.3 CHECK
CHECK evaluates a condition and skips the current loop iteration if the condition is false.
LOOP AT lt_numbers INTO DATA(lv_num).
CHECK lv_num > 1.
WRITE lv_num.
ENDLOOP.
CHECK helps implement early-exit logic, reducing nested IF statements.
4. Nested Logic & Best Practices
Deeply nested code is difficult to read and maintain.
4.1 Bad Practice (Deep Nesting)
IF lv_a = 1.
IF lv_b = 2.
IF lv_c = 3.
WRITE 'Too deep'.
ENDIF.
ENDIF.
ENDIF.
4.2 Best Practice: Early Exit
CHECK lv_a = 1.
CHECK lv_b = 2.
CHECK lv_c = 3.
WRITE 'Clean and readable logic'.
Flatten your logic. Readable code is maintainable code.
4.3 Use CASE Instead of IF Chains
CASE lv_status.
WHEN 'S'.
WRITE 'Success'.
WHEN 'E'.
WRITE 'Error'.
WHEN 'W'.
WRITE 'Warning'.
ENDCASE.
4.4 Avoid Database Access Inside Loops
❌ Bad Practice
LOOP AT lt_data INTO DATA(ls_data).
SELECT SINGLE * FROM dbtab INTO @DATA(ls_db)
WHERE id = ls_data-id.
ENDLOOP.
✅ Good Practice
SELECT * FROM dbtab INTO TABLE @DATA(lt_db).
LOOP AT lt_data INTO DATA(ls_data).
READ TABLE lt_db INTO DATA(ls_db)
WITH KEY id = ls_data-id.
ENDLOOP.
Never perform SELECT statements inside loops unless absolutely unavoidable.
5. Common Beginner Mistakes
Avoid These:
-
Forgetting ENDIF / ENDLOOP
-
Creating infinite loops
-
Excessive nesting
-
Using EXIT instead of RETURN
-
Mixing business logic inside loops
6. Summary
-
Use IF for complex conditions
-
Use CASE for cleaner branching
-
Choose loops wisely
-
Use CHECK for early exits
-
Reduce nesting
-
Avoid database access inside loops
7. Practice Exercises
-
Print even numbers from 1 to 20 using a loop.
-
Skip numbers divisible by 3 using CONTINUE.
-
Exit a loop when a value exceeds 50.
-
Rewrite nested IF logic using CHECK.
8. What’s Next?
➡️ Module 5: Internal Tables & Work Areas
If you master control structures, debugging and performance tuning become much easier later.