BC170_2.06.1
Control Statements
BC170_2.06.2
Objectives
•
The participants will be able to:
–
Use the basic Control Flow Constructs that
are available in the ABAP Editor
–
Use the following statements in an ABAP
Program
•
IF, CASE, DO, WHILE, ON CHANGE OF,
CHECK, EXIT, and CONTINUE
–
Use the Logical Expressions that are
available in the ABAP Editor
BC170_2.06.3
Basic Flow Control in ABAP
BC170_2.06.4
The IF Statement
IF X = 5.
WRITE:/ ‘The value of X is 5’.
ELSEIF X = 6.
WRITE:/ ‘The value of X is 6’.
ELSE .
WRITE:/ ‘X is neither 5 nor 6’.
ENDIF.
BC170_2.06.5
Logical Expressions
Logical Expressions use :
–
RELATIONAL OPERATORS
–
LOGICAL OPERATORS
–
STRING COMPARISON OPERATORS
BC170_2.06.6
Comparison
Syntax
Is Equal to
=, EQ
Is not equal to
< >, ><, NE
Greater than
>, GT
Greater than or equal to
> =, = >, GE
Less than
<, LT
Less than or equal to
<=, =<, LE
Relational Operators
BC170_2.06.7
Logical Operators
NOT
AND
OR
The hierarchy of the logical
operators is:
NOT, AND, and then OR.
(i.e., different from creating views)
BC170_2.06.8
Good Programming Practice
with
Logical Operators
If not ( X = 0 )
or not ( Y = 1
and
Z = X or X = 3
and ( Z = 5 )
Bad
BC170_2.06.9
String Comparison Operators
Comparison
Syntax
Contains only
Contains any
Contains string
Contains pattern
Contains not only
Contains not any
Contains no string
Contains no pattern
CO
CA
CS
CP
CN
NA
NS
NP
BC170_2.06.10
The CASE Statement
DATA: year(4) type c.
…..some code to fill year variable with
data…..
CASE year.
WHEN ‘1997’.
FORMAT COLOR 6.
WHEN ‘1998’.
FORMAT COLOR 2.
WHEN OTHERS.
FORMAT COLOR 1.
ENDCASE.
BC170_2.06.11
The DO Loop
DO.
WRITE :/ ‘Hello world!’.
ENDDO.
J =4.
DO J TIMES.
WRITE :/ ‘Hello world!’.
ENDDO.
BC170_2.06.12
The WHILE Loop
If expression evaluates
to TRUE, code in loop is
executed.
If expression evaluates to
FALSE, code in loop is NOT
executed, and control
moves to after ENDWHILE.
BC170_2.06.13
Nested Loops and Control
Structures
DO 2 TIMES.
WRITE :/ SY-INDEX.
DO 3 TIMES.
WRITE : / ‘ ‘, SY-INDEX.
ENDDO.
ENDDO.
The source code above would display:
1
1
2
3
2
1
2
3
BC170_2.06.14
The ON CHANGE OF
Statement
SELECT * FROM YCUSTOMER.
ON CHANGE OF YCUSTOMER-COUNTRY
OR
YCUSTOMER-STATE.
[….]
ENDON.
ENDSELECT.
BC170_2.06.15
The CHECK Statement
DO 10 TIMES.
CHECK SY-INDEX <= 4.
WRITE :/ SY-INDEX.
ENDDO.
BC170_2.06.16
The EXIT Statement
IF SY-SUBRC <>0.
EXIT.
ENDIF.
BC170_2.06.17
The CONTINUE Statement
DO 10 TIMES.
IF SY-INDEX >4.
CONTINUE .
ENDIF.
WRITE :/ SY-INDEX.
ENDDO.
BC170_2.06.18
Summary
•
The participants should be able to:
–
Use the basic Control Flow Constructs that
are available in the ABAP Editor
–
Use the following statements in an ABAP
Program
•
IF, CASE, DO, WHILE, ON CHANGE OF,
CHECK, EXIT, and CONTINUE
–
Use the Logical Expressions that are
available in the ABAP Editor