IBM Global Services
Control Statements
Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
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, CHECK, EXIT, and CONTINUE
Use the Logical Expressions that are available in the ABAP Editor
2 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Basic Flow Control in ABAP
3 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
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.
4 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Logical Expressions
Logical Expressions use :
RELATIONAL OPERATORS
LOGICAL OPERATORS
STRING COMPARISON OPERATORS
5 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Relational Operators
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
6 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Logical Operators
AND
OR
NOT
The hierarchy of the logical
operators is: NOT, AND and
then OR. (i.e. different from
creating views)
7 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Bad Programming Practice with Logical Operators
If not ( X = 0 )
or not ( Y = 1 and
Z = X or X = 3
and ( Z = 5 )
8 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
String Comparison Operators
Comparison Syntax
Contains only CO
Contains any CA
Contains string CS
Contains pattern CP
Contains not only CN
Contains not any NA
Contains no string NS
Contains no pattern NP
9 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Demonstration
Writing an ABAP program with the ‘IF’ statement with logical operators.
10 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Practice
Writing an ABAP program with the ‘IF’ statement with logical operators.
11 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
The CASE Statement
hen
W
V E ’.
‘SA n
CASE Whe .
RTA’
SY-UCOMM. ‘S
When
‘SRTD’.
When
Wh ‘GET
en D’.
‘ P IC
Don’t forget K’.
those periods!
12 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Demonstration
Writing an ABAP program with the ‘CASE’ statement.
13 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Practice
Writing an ABAP program with the ‘CASE’ statement.
14 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
The DO Loop
J =4.
DO. DO J TIMES.
WRITE :/ ‘Hello world!’. WRITE :/ ‘Hello world!’.
ENDDO. ENDDO.
15 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
The WHILE Loop
If expression evaluates to
FALSE, code in loop is NOT
If expression evaluates executed, and control moves
to TRUE, code in loop is to after ENDWHILE.
executed.
16 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Nested Loops and Control Structures
DO 2 TIMES.
WRITE :/ SY-INDEX.
DO 3 TIMES.
WRITE : / ‘ ‘, SY-INDEX.
ENDDO.
ENDDO.
17 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
The CHECK Statement
DO 10 TIMES.
CHECK SY-INDEX <= 4.
WRITE :/ SY-INDEX.
ENDDO.
18 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
The EXIT Statement
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
19 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
The CONTINUE Statement
DO 10 TIMES.
IF SY-INDEX >4.
CONTINUE .
ENDIF.
WRITE :/ SY-INDEX.
ENDDO.
20 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Demonstration
Writing an ABAP program and work with the ‘LOOP’ statement.
21 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Practice
Writing an ABAP program and work with the ‘LOOP’ statement.
22 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Summary
Logical expressions are constructed using relational, logical, and string
comparison operators.
The CASE statement is used to distinguish between mutually exclusive options.
A DO loop is used to unconditionally execute a block of code multiple times.
WHILE loop conditionally executes a block of code, possibly multiple times.
The CHECK statement is used to test a logical expression
The EXIT statement unconditionally terminates a loop, subroutine, or program.
The CONTINUE statement is used inside a loop.
23 Control Statements | Dec-2008 © 2005 IBM Corporation
IBM Global Services
Questions
What are the different kind of control statements in ABAP?
What are different logical expressions available in ABAP?
24 Control Statements | Dec-2008 © 2005 IBM Corporation