Most common control structure there is, that can check multiple fields. Key logic statement to determine flow of program. Can use the following operators: =, <>, <, >, <=, >= or EQ, NE, LT, GT, LE, GE. We can link logical expressions with the OR, AND or NOT operators. The code we execute between IF and ENDIF, can be anything at all. Can nest statements as deep as you wish. Limit logic expressions linked together for readability, instead use multiple single logical expressions or make it at least easier on the eye. CASE statement is more readable, but is limited to one logical expression and checking one field.
*IF logical expression
* Do something
*endif
*
DATA: surname(15) TYPE c,
forename(15) TYPE c.
surname = 'BROWN'.
forename = 'JOHN'.
IF surname = 'SMITH'.
WRITE 'Youve won a car!'.
ELSEIF surname = 'BROWN'.
WRITE 'Youve won a boat!'.
ELSEIF surname = 'JONES'.
WRITE 'Youve won a PLANE!'.
ELSEIF surname = 'ANDREWS'.
WRITE 'Youve won a HOUSE!'.
ELSE.
WRITE 'Unlucky! You go home empty handed'.
ENDIF.
*************************************************
* Nested-IF Statement
IF surname = 'SMITH'.
WRITE 'Youve won a car!'.
WRITE 'Youve also won another car!'.
IF forename = 'JOHN'.
WRITE 'Youve won a third car!'.
WRITE 'Youve also won another car!'.
IF location = 'UK'.
WRITE 'Youve won a fifth car!'.
WRITE 'Youve also won another car!'.
ELSE.
WRITE 'Oooo, so close: no more cars for you'.
ENDIF.
WRITE 'Youve won another car!'.
ENDIF.
ENDIF.
*************************************************
* Linked logical expression
*
IF surname = 'SMITH' OR forename = 'JOHN'.
WRITE 'Youve won a car!'.
ENDIF.
*************************************************
* Nested CASE Statement
*
CASE surname.
WHEN 'SMITH'.
WRITE 'Youve won a car!'.
CASE forname.
WHEN 'BARRY'.
WRITE 'Hi Barry'.
WHEN 'PAUL'.
WRITE 'Hi Paul'.
WHEN OTHERS.
WRITE 'Who are you?'.
ENDCASE.
WHEN 'JONES'.
WRITE 'Youve won a PLANE!'.
WHEN 'GREEN'.
WRITE 'Youve won a BOAT!'.
WHEN OTHERS.
WRITE 'Unlucky!'.
ENDCASE.