Control Statements

To control the flow of the ABAP program use the following statements.

IF – Branching Conditionally
IF statement – The code between IF and ENDIF is executed only if the condition is true.

DATA: a TYPE i VALUE 10.  " We can assign a value in the declaration 

IF a > 5.
  WRITE:/ 'Condition True'.
ENDIF.

Output
Control-Statements-1

IF-ELSE statement – The code between IF and ELSE is executed if the condition is true, the code between ELSE and ENDIF is executed if the condition is False.

DATA: a TYPE i VALUE 1.

IF a > 5.
  WRITE:/ 'Condition True'.
ELSE.
  WRITE:/ 'Condition False'.
ENDIF.

Output
Control-Statements-2

IF-ELSEIF statement – Used to check multiple conditions.

DATA: a TYPE i VALUE 2.

IF a > 5.
  WRITE:/ a, 'Greater Than', 5.
ELSEIF a > 4.
  WRITE:/ a, 'Greater Than', 4.
ELSEIF a > 3.
  WRITE:/ a, 'Greater Than', 3.
ELSE.
  WRITE:/ a, 'Less Than', 3.
ENDIF.

Output
Control-Statements-3

CASE-ENDCASE – Branching based on the content of the variable.

DATA: a TYPE i VALUE 4.

CASE a.
  WHEN 3.
    WRITE:/ a, 'Equals', 3.
  WHEN 4.
    WRITE:/ a, 'Equals', 4.
  WHEN OTHERS.
    WRITE:/ 'Not Found'.
ENDCASE.

Output
Control-Statements-4

When no condition is met, OTHERS will be executed. OTHERS is not mandatory.


6 Comments

  1. @harsh surya ..”don’t say bakvas if u really have stuff then contribute else dont comment.
    @saphub ….”very good material for the starters”

  2. Small mistake in this line
    DATA: a TYPE i VALUE 10. ” We can assign a value in the declaration
    Should have used * instead of “

    • thats not mistake but correct to make a comment part in line used (“). so not all of that line to be comment. But if u wanna make 1 line to be comment so used (*) 🙂
      Please try first and u will see thats its correct

Comments are closed.