07 - ABAP - Common Control Statements
07 - ABAP - Common Control Statements
Contents
Objectives Control Statements
Using the IF Statement Using the CASE Statement Using the EXIT Statement Using the DO Statement Using the WHILE Statement Using the CONTINUE Statement Using the CHECK Statement Comparing the EXIT, CONTINUE, and CHECK Statements
Summary
Q&A
Contents
Objectives
Objectives
Code the common control statements IF, CASE, DO, and WHILE Control program sequence using EXIT, CONTINUE, and CHECK Code simple position and length specifications on the WRITE statement
Contents
Using the IF Statement
IF Statement
Syntax:
if [not] exp [ and [not] exp ] [ or [not] exp ]. --[elseif exp. ---] [else. ---] endif.
where:
exp is a logical expression that evaluates to a true or false condition. --- represents any number of lines of code. Even zero lines are allowed.
IF Statement
IF Statement
Data Conversion of Literals During Comparisons
IF Statement
REPORT y_vu_1001. DATA: BEGIN OF s1, x VALUE 'X', y VALUE 'Y', z VALUE 'Z', END OF s1, BEGIN OF s2, x VALUE 'X', z VALUE 'Z', END OF s2. IF s1-x = s2-x. WRITE: / s1-x, '=', s2-x. ELSE. WRITE: / s1-x, '<>', s2-x. ENDIF. IF s1 = s2. "comparing field strings byte by byte WRITE: / 's1 = s2'. ELSE. WRITE: / 's1 <> s2'. ENDIF.
IF 0 = ' '. "Watch out for this one WRITE: / '0 = '' '''. ELSE. WRITE: / '0 <> '' '''. ENDIF.
IF s1-x BETWEEN s2-x AND s2-z. WRITE: / s1-x, 'is between', s2-x, 'and', s2-z. ELSE. WRITE: / s1-x, 'is not between', s2-x, 'and', s2-z. ENDIF.
Using ELSEIF
to avoid nesting IFs
IF f1 = f2. WRITE: / f1, '=', f2. ELSEIF f1 = f3. WRITE: / f1, '=', f3. ELSEIF f2 = f3. WRITE: / f2, '=', f3. ELSE. WRITE: / 'all fields are different'. ENDIF. IF f1 = f2. WRITE: / f1, '=', f2. ELSE. IF f1 = f3. WRITE: / f1, '=', f3. ELSE. IF f2 = f3. WRITE: / f2, '=', f3. ELSE. WRITE: / 'all fields are different'. ENDIF. ENDIF. ENDIF.
'AXCZ' ca 'AB'
'ABCD' ca 'XYZ' 'AXCZ' na 'ABC' 'ABCD' na 'XYZ'
True
False False True
CP contains pattern
Contents
Using the CASE Statement
CASE Statement
Syntax:
case v1. when v2 [ or vn ... ]. --when v3 [ or vn ... ]. --[ when others. --- ] endcase.
where:
v1 or v2 can be a variable, literal, constant, or field string. --- represents any number of lines of code. Even zero lines are allowed.
CASE Statement
REPORT y_vu_1005. PARAMETERS f1 TYPE i DEFAULT 2. CASE f1. WHEN 1. WRITE / 'f1 = 1'. WHEN 2. WRITE / 'f1 = 2'. WHEN 3. WRITE / 'f1 = 3'. WHEN OTHERS. WRITE / 'f1 is not 1, 2, or 3'. ENDCASE.
Contents
Using the EXIT Statement
EXIT Statement
Syntax: exit. prevents further processing from occurring
in main program: exits program inside LOOP, SELECT, DO, WHILE: leaves loop processing inside subroutine: leaves subroutine
REPORT y_vu_1006. WRITE: / 'This line is showed.'. EXIT. WRITE: / 'This line is not showed.'.
Contents
Using the DO Statement
DO Statement
Syntax:
do [ v1 times ] --[exit.] --enddo.
where:
v1 is a variable, literal, or constant.
DO Statement
sy-index = 99. WRITE: / 'before loop, sy-index =', sy-index, / ''. DO 5 TIMES. WRITE sy-index. ENDDO. WRITE: / 'after loop, sy-index =', sy-index, / ''. DO 4 TIMES. WRITE: / 'outer loop, sy-index =', sy-index. DO 3 TIMES. WRITE: / ' inner loop, sy-index =', sy-index. ENDDO. ENDDO. WRITE: / ''. DO 10 TIMES. WRITE sy-index. IF sy-index = 3. EXIT. ENDIF. ENDDO. "new line
Contents
Using the WHILE Statement
WHILE Statement
similar to DO statement Syntax:
while exp. --[ exit. ] --endwhile.
where:
exp is a logical expression.
Contents
Using the CONTINUE Statement
CONTINUE Statement
only within a loop
terminates the current loop pass jumps to the beginning of the next loop pass
DO 10 TIMES. IF sy-index BETWEEN 3 AND 8. CONTINUE. ENDIF. WRITE sy-index. ENDDO.
Contents
Using the CHECK Statement
CHECK Statement
Syntax: CHECK exp. within a loop
If exp is TRUE: does nothing If exp is FALSE: same as CONTINUE
terminates the current loop pass jumps to the beginning of the next loop pass
Contents
Comparing the EXIT, CONTINUE, and CHECK Statements
Contents
Simple Position and Length Specifications for the WRITE Statement
WRITE Statement
Syntax:
write [/][P][(L)] v1.
where:
v1 is a variable, literal, or constant. P is a number indicating a position on the current line of output. L is a number indicating the number of bytes allocated in the list for outputting v1.
WRITE Statement
WRITE Statement
WRITE: / '....+....1....+....2....+....3....+....4', /1 'Hi', 4 'There', /1 'Hi', 3 'There', /1 'Hi', 2 'There', /1 'Hi', 1 'There', /2 'Hi', 'There', /2(1) 'Hi', 'There', /2(3) 'Hi', 'There', /2 'Hi', 10(3) 'There'. ....+....1....+....2....+....3....+....4 Hi There HiThere HThere There Hi There H There Hi There Hi The
Contents
Summary
Summary
Comparison statements: IF and CASE
CASE: compares 2 values only and only for equality Conversions occur when comparing differing data types string comparisons
CP and NP to match strings against patterns SY-FDPOS is set after each comparison
Summary