Академический Документы
Профессиональный Документы
Культура Документы
Lesson 3
Lesson Objectives
Flow of Control
Logical Expressions
IF Statement
CASE Statement
DO Loop
WHILE Loop
EXIT Statement
Debugging Programs
Debugging Mode
Debugger Views
Breakpoint
Setting Breakpoints
Program Breakpoint
Breakpoint Table
Summary
Summary
Summary
Summary
Quick Check
Quick Check
Quick Check
Quick Check
Slide
1 of
32
Lesson Purpose
The purpose of this lesson is to examine and describe the use of control
statements with the use of data in ABAP/4 programs.
In learning to write ABAP/4 programs, you need to learn how to use conditional
logic to perform the processing of alternative sets of statements within a program.
You also need to learn how to use the ABAP/4 debug feature to help you track
down errors in your program logic.
Slide
2 of
32
Lesson Topics
o Flow of Control
o Logical Expressions
o Conditional statements
IF/ELSEIF/ELSE/ENDIF
CASE/WHEN/ENDCASE
DO/ENDDO
WHILE/ENDWHILE
CONTINUE, CHECK, EXIT
o Debugging programs using the ABAP/4 Debugger
o Using Breakpoints with programs
Slide
3 of
32
Lesson Objectives
Flow of Control
However, ABAP/4 differs from these other programming languages in that it has
internal control and external control of the program flow.
Slide
5 of
32
Suppose a report program which generates a list and provides some drill-
down facilities has to react when a user selects a line (for further
information about this, see Interactive Lists). The code which needs to be
processed for that event must be inserted after the event keyword AT
LINE-SELECTION.
AT LINE-SELECTION.
MOVE 'X' TO FLAG.
.....
Whenever the user selects an item in the list by clicking the mouse or by
pressing F2, all the statements between AT LINE-SELECTION and the
next event keyword are processed. External control events are considered
in more detail in subsequent lessons.
Slide
6 of
32
Logical Expressions
You control the internal flow of your program with conditions. To formulate
conditions, use logical expressions which compare data fields as follows:
Syntax
.... <f1> <operator> <f2> ...
This expression compares two fields. The result of the comparison can only be
either true or false. You use logical expressions in condition statements with the
keywords IF, CHECK, and WHILE. Depending on the data types of the operands
<f1> and <f2>, you can use different logical operators.
Also, you can perform tests to check whether data fields fulfill certain criteria:
In addition, you can combine several logical expressions into one single logical
expression:
For comparisons with all field types, you can use the following
operators in logical expressions:
<operator> Meaning
EQ equal to
= equal to
NE not equal to
<> not equal to
>< not equal to
LT less than
< less than
LE less than or equal to
<= less than or equal to
GT greater than
> greater than
GE greater than or equal to
>= greater than or equal to
If it makes sense, you can compare fields of different data types. Fields
can be compared if they are convertible. Before performing the
comparison, the system executes an automatic type conversion
according to the following hierarchical rules:
For comparisons with character strings (type C) and numeric text (type N), you can
use the following operators in logical expressions
<operator> Meaning
CO Contains Only
CN Contains Not only
CA Contains Any
NA contains Not Any
CS Contains String
NS contains No String
CP Contains Pattern
NP contains No Pattern
CO (Contains Only)
is true if <f1> contains only characters from <f2>. The comparison is case-
sensitive. Trailing blanks are included. If the comparison is true, the system field
SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the
offset of the first character of <f1> that does not occur in <f2>.
<f1> CN <f2>
is true if <f1> does also contains characters other than those in <f2>. The
comparison is case-sensitive. Trailing blanks are included. If the comparison is
true, the system field SY-FDPOS contains the offset of the first character of <f1>
that does not also occur in <f2>. If it is false, SY-FDPOS contains the length of
<f1>.
CA (Contains Any)
<f1> CA <f2>
is true if <f1> contains at least one character from <f2>. The comparison is case-
sensitive. If the comparison is true, the system field SY-FDPOS contains the offset
of the first character of <f1> that also occurs in <f2>. If it is false, SY-FDPOS
contains the length of <f1>.
<f1> NA <f2>
is true if <f1> does not contain any character from <f2>. The comparison is case-
sensitive. If the comparison is true, the system field SY-FDPOS contains the length
of <f1>. If it is false, SY-FDPOS contains the offset of the first character of <f1>
that occurs in <f2>.
CS (Contains String)
is true if <f1> contains the character string <f2>. Trailing blanks are ignored and
the comparison is not case-sensitive. If the comparison is true, the system field SY-
FDPOS contains the offset of <f2> in <f1>. If it is false, SY-FDPOS contains the
length of <f1>.
NS (contains No String)
<f1> NS <f2>
is true if <f1> does not contain the character string <f2>. Trailing spaces are
ignored and the comparison is not case-sensitive. If the comparison is true, the
system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS
contains the offset of <f2> in <f1>.
CP (Contains Pattern)
<f1> CP <f2>
is true if <f1> contains the pattern <f2>. If <f2> is of type C, you can use the
following wildcards in <f2>:
Trailing blanks are ignored and the comparison is not case-sensitive. If the
comparison is true, the system field SY-FDPOS contains the offset of <f2> in
<f1>. If it is false, SY-FDPOS contains the length of <f1>.
NP (contains No Pattern)
The logical expression
<f1> NP <f2>
is true if <f1> does not contain the pattern <f2>. In <f2>, you can use the same
wildcards and escape character as for the operator CP.
Trailing blanks are ignored and the comparison is not case-sensitive. If the
comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is
false, SY-FDPOS contains the offset of <f2> in <f1>.
The following table shows the results of executing this program, depending on
which operators and F1 / F2 fields you use.
To compare the bit structure of the first byte of the initial operand of a
logical expression with the bit structure of the second operand, use the
following operators.
<operator> Meaning
O bits are one
Z bits are zero
M bits are mixed
<f> O <hex>
<f> M <hex>
is true if from the bit positions that are 1 in <hex>, at least one is 1 and
one is 0 in <f>.
00 0
01 1
02 2
03 3
40 64
41 65
42 66
43 67
The bit structure of the character 'C' is defined for the current hardware
platform by its ASCII code number 67. As you see, the numbers which
occur in the list output are those where the bit positions filled with 1
are the same as in the bit structure of 67.
Syntax
This expression is true if <f1> occurs in the range between <f2> and
<f3>. It is a short form of the following logical expression:
To check whether a field is set to its initial value, you use a logical expression with the IS
INITIAL parameter as follows.
Syntax
This expression is true if the field <f> contains the initial value for its type. In general,
any field, elementary or structured (field strings and internal tables), contains its initial
value after a CLEAR <f> statement is executed (see Resetting Values to Default Values).
Here, the character string FLAG does not contain its initial value after the DATA
statement because it is set to 'X' with the VALUE parameter. When the CLEAR
statement is executed, it is reset to its initial value.
Combining Several Logical Expressions
You can combine several logical expressions together in one single expression by using
the logical link operators AND, OR, and NOT:
NOT takes priority over AND, and AND takes priority over OR. However, you can use
any combination of parentheses to specify the processing sequence. Since ABAP/4
interprets parentheses as individual words, they must be preceded and followed by at
least one blank.
ABAP/4 processes logical expressions from left to right. If it recognizes one of the
component expressions as true or false, it does not perform the remaining comparisons or
checks in this component. This means that you can improve performance by organizing
logical expressions in such a way that you place comparisons which are often false at the
beginning of an AND chain and expensive comparisons, such as searches for character
strings, at the end.
Slide
8 of
32
IF Statement
Syntax
IF <condition1>.
<statement block>
ELSEIF <condition2>.
<statement block>
ELSEIF <condition3>.
<statement block>
.....
ELSE.
<statement block>
ENDIF.
If the first condition is true, the system executes all the statements up
to the end of the first statement block and then continues processing
after the ENDIF statement. To introduce alternative conditions, you
can use ELSEIF statements. If the first condition is false, the system
processes the following ELSEIF statement in the same way as the IF
statement. ELSE begins a statement block which is processed if none
of the IF or ELSEIF conditions is true. The end of the last statement
block must always be concluded by ENDIF.
Condition 2 is fulfilled.
CASE Statement
The system executes the statement block after the WHEN statement if the
contents of <> equals the contents of <>, and continues processing after the
ENDCASE statement. If the first WHEN is not processed then the next one is
evaluted and processed is an equal condition exists. The statement block after the
optional WHEN OTHERS statement is executed if the contents of <> does not
equal any of the <> contents. The last statement block must be concluded with
ENDCASE.
String is not X Y Z
Here, the last statement block after WHEN OTHERS is processed because
the contents of STRING, "A", does not equal "X", "Y", or "Z".
Syntax
CASE <f>.
WHEN <f1>.
<statement block>
WHEN <f2>.
<statement block>
WHEN <f3>.
<statement block>
WHEN ...
......
WHEN OTHERS.
<statement block>
ENDCASE.
The system executes the statement block after the WHEN statement if
the contents of <f> equals the contents of <fi>, and continues
processing after the ENDCASE statement. The statement block after
the optional WHEN OTHERS statement is executed if the contents of
<f> does not equal any of the <fi> contents. The last statement block
must be concluded with ENDCASE.
IF <f> = <f1>.
<statement block>
ELSEIF <f> = <f2>.
<statement block>
ELSEIF <f> = <f3>.
<statement block>
ELSEIF <f> = ...
...
ELSE.
<statement block>
ENDIF.
DO Loop
You can limit the number of times the loop is processed with the TIMES option.
<n> can be literal or a variable. If <n> is 0 or negative, the system does not
process the loop.
The system field SY-INDEX contains the number of times the loop has been
processed.
o The <n> TIMES parameter is optional. If you do not specify it, you need
to build a termination condition into the loop (see EXIT statement).
o SY-INDEX is the loop counter for the loop commands DO and WHILE.
SY-INDEX has the value 1 during the first loop pass and is increased by 1
by the system for each loop pass.
o Modifying the contents of SY-INDEX will NOT alter the number of loop
passes within DO ... ENDDO. SY-INDEX is maintained by the ABAP
processor and is set to the appropriate value at the top of the DO …
ENDDO loop.
o DO loops may be nested. SY-INDEX is associated only with the current
nested looping construct.
Syntax
CASE <f>.
WHEN <f1>.
<statement block>
WHEN <f2>.
<statement block>
WHEN <f3>.
<statement block>
WHEN ...
......
WHEN OTHERS.
<statement block>
ENDCASE.
The system executes the statement block after the WHEN statement if
the contents of <f> equals the contents of <fi>, and continues
processing after the ENDCASE statement. The statement block after
the optional WHEN OTHERS statement is executed if the contents of
<f> does not equal any of the <fi> contents. The last statement block
must be concluded with ENDCASE.
IF <f> = <f1>.
<statement block>
ELSEIF <f> = <f2>.
<statement block>
ELSEIF <f> = <f3>.
<statement block>
ELSEIF <f> = ...
...
ELSE.
<statement block>
ENDIF.
1:
DO.
WRITE SY-INDEX.
IF SY-INDEX = 3.
EXIT.
ENDIF.
ENDDO.
1 2 3
Here, the processing passes through the loop three times and then leaves it
after the EXIT statement.
2:
You can nest DO loops as many times as you like and also combine them
with other loops.
This example shows two nested loops, both using the TIMES option.
DO 2 TIMES.
WRITE SY-INDEX.
SKIP.
DO 3 TIMES.
WRITE SY-INDEX.
ENDDO.
SKIP.
ENDDO.
1
1 2 3
2
1 2 3
The outer loop is processed twice. Each time the outer loop is processed,
the inner loop is processed three times. Note that the system field SY-
INDEX contains the number of loop passes for each loop individually.
3:
You can assign new values to a variable <> in each loop pass by using the
VARYING option. <f1>, <f2>, <f3>,... must be a series of equidistant
fields of the same type and length in memory. In the first loop pass, <f1>
is assigned to <f> in the second loop pass <f2> is assigned to <f> and so
on. You can use several VARYING options in one DO statement.
If you change the control variable <f> inside the DO loop, the system
changes the corresponding field <fi> automatically.
Caution!
Ensure that you do not process the loop more times than the number of variables
<f1>, <f2>, <f3>,... involved, since this may result in a runtime error.
This is a loop
This was a loop
If you want to process a statement block more than once, you can program a loop with
the DO statement as follows:
Syntax
The system continues processing the statement block introduced by DO and concluded by
ENDDO until it finds an EXIT, STOP, or REJECT statement (see Terminating Loops).
Terminating Loops
Keyword Purpose
CONTINUE Terminating a Loop Pass Unconditionally
CHECK Terminating a Loop Pass Conditionally
EXIT Terminating a Loop Entirely
You can only use CONTINUE in loops, but the keywords CHECK and
EXIT can also be used outside loops. There, they fulfill different
functions. For example, they can terminate subroutines or an entire
processing block. For more information about the CHECK and EXIT
statements and how they work outside loops, see Terminating
Subroutines and Terminating Processing Blocks .
To terminate a loop pass immediately without any condition, use the CONTINUE
statement as follows:
Syntax
CONTINUE.
After a CONTINUE statement, the system skips all the remaining statements in the
current statement block and continues with the next loop pass.
DO 4 TIMES.
IF SY-INDEX = 2.
CONTINUE.
ENDIF.
WRITE SY-INDEX.
ENDDO.
Here, the system terminates the second loop pass without processing the WRITE
statement.
You can limit the number of times the loop is processed with the TIMES option. <n> can
be literal or a variable. If <n> is 0 or negative, the system does not process the loop.
The system field SY-INDEX contains the number of times the loop has been processed.
Caution!
Avoid endless loops when working with the DO statement. If you do not
use the TIMES option, include at least one EXIT, STOP, or REJECT
statement in the statement block so that the system can leave the loop.
Syntax
CHECK <condition>.
If the condition is false, the system skips all the remaining statements
in the current statement block and continues with the next loop pass.
For <condition>, use any logical expression described in Programming
Logical Expressions .
DO 4 TIMES.
CHECK SY-INDEX BETWEEN 2 and 3.
WRITE SY-INDEX.
ENDDO.
2 3
Here, the system terminates the first and the fourth loop pass without
processing the WRITE statement because SY-INDEX does not fall
between 2 and 3.
Slide
11 of
32
WHILE Loop
If you want to process a statement block more than once as long as a condition is
true, you can program a loop with the WHILE statement.
TestString
STRLEN: 11
Length of string: 11
Syntax
The system field SY-INDEX contains the number of times the loop
has been processed.
You can nest WHILE loops any number of times and also combine
them with other loops.
The VARY option of the WHILE statement works in the same way as
the VARYING option of the DO statement (see Unconditional
Looping using DO). It allows you to assign new values to a variable
<f> each time the loop is processed. <f1>, <f2>, <f3>,... must be a
series of equidistant fields of the same type and length in memory. In
the first loop pass, <f1> is assigned to <f>; in the second loop pass
<f2> is assigned to <f>; and so on. You can use several VARY options
in one WHILE statement.
Caution!
Avoid endless loops when working with the WHILE statement.
Remember that the condition in the WHILE statement should
become false at some time or that the system should be able to
leave the loop by finding an EXIT, STOP, or REJECT
statement.
Slide
12 of
32
You can only program the CONTINUE statement within a loop. The command
ensures that subsequent statements within the loop are not executed in this pass
and the next loop pass begins immediately.
The CHECK statement within a loop structure has the same effect as CONTINUE
if the specified logical expression is not fulfilled.
If a CHECK condition is not currently fulfilled within a subroutine, the subroutine
terminates.
If a CHECK statement occurs outside a loop structure and its logical expression is
not fulfilled, the following statements in the current processing block are not
executed.
Syntax
CONTINUE.
DO 4 TIMES.
IF SY-INDEX = 2.
CONTINUE.
ENDIF.
WRITE SY-INDEX.
ENDDO.
1 3 4
Here, the system terminates the second loop pass without processing
the WRITE statement.
Syntax
CHECK <condition>.
If the condition is false, the system skips all the remaining statements
in the current statement block and continues with the next loop pass.
For <condition>, use any logical expression described in Programming
Logical Expressions .
DO 4 TIMES.
CHECK SY-INDEX BETWEEN 2 and 3.
WRITE SY-INDEX.
ENDDO.
2 3
Here, the system terminates the first and the fourth loop pass without
processing the WRITE statement because SY-INDEX does not fall
between 2 and 3.
Slide
13 of
32
EXIT Statement
You use the EXIT statement within a loop structure to leave the current loop.
You use the EXIT statement within a subroutine to leave the current subroutine.
If you use the EXIT statement outside a loop or a subroutine, it causes the report
processing to terminate, and the list is output (this does not apply for AT events.
See the “Reporting” lesson).
Syntax
EXIT.
After an EXIT statement, the system immediately leaves the loop and
continues the processing after the closing statement (ENDDO,
ENDWHILE, ENDSELECT). Within nested loops, the system only
leaves the current loop.
DO 4 TIMES.
IF SY-INDEX = 3.
EXIT.
ENDIF.
WRITE SY-INDEX.
ENDDO.
1 2
Here, the system terminates the entire loop processing in the third loop
pass without processing the WRITE statement or the fourth loop pass.
Slide
14 of
32
Debugging Programs
The ABAP/4 Debugger lets you stop a program during runtime and examine the
flow and results of each statement during execution. Stepping through a program
with the Debugger helps you to detect and correct errors in your code.
· Set watchpoints.
· Stop a program at specific key words or events or when a fields contents change.
Slide
15 of
29
There are several different ways you can branch to debugging mode:
1. Start a program by calling the Debugging function (from the ABAP/4 Editor
initial screen).
2. Set breakpoints in the program (choose Utilities Breakpoints Set) and
start the program (choose Program Execute).
3. If you want to test a transaction (screen debugging), enter /h in the command
field, press ENTER and choose Execute.
4. From the Object Browser, you can access debugging mode by choosing Program
and Display from the Object list box or Program objects and Edit, and then
pressing the pushbutton Test/execute. On the next screen, you can specify the
execution type (e.g. Debugging).
Slide
16 of
32
Debugging Mode
• When you are in debugging mode, the program lines appear in the upper half of
the screen. The next line to be processed is flagged by the character >. As usual,
you can scroll through the program code using the standard function keys.
• In the lower half of the screen, you can display the contents of fields. To do this,
you place the cursor on a field and click Choose or use the standard selection
actions (press F2 or double-click).
Slide
17 of
32
Debugger Views
When you debug a program, you can choose between six different views in the
Debugger. You switch between these views by selecting the appropriate view
name in the Debugger's Goto menu or by pressing the small pushbuttons at the
top right-hand corner of each debugging screen. These buttons have the following
meanings:
The last button on the right is the + (plus sign). Use this button to turn on or off
the display of the Variables text group. This group appears in the Debugging
View.
Slide
18 of
32
When you execute a program that contains breakpoints, the system automatically
interrupts the program and places you in the Debugger when it encounters a
breakpoint. You do not have to set breakpoints to start the Debugger. Instead, you
can execute a program in 'debugging mode'.
Replace function
You assign a new value to a field by entering the value in the appropriate place.
Next, you select the checkbox in the column R and press the pushbutton. The new
value is then used in the debugging session.
Continue function
The Debugger executes commands up to the next active breakpoint. You set the
breakpoints as described in subsequent slides. If no further breakpoints exist, the
system executes the report in its entirety without stopping.
Single step function
Execute a program statement by statement. If you select Single step while on a
line that calls a subroutine, for example, the next mouse click carries you into the
called subroutine. After stepping your way through the subroutine, you return to
the line of code directly following the subroutine call.
Execute function
Process a program line by line. If you choose Execute while on a line that calls a
subroutine, the debugger executes the subroutine and halts at the line of code
directly following the call. You thus skip over the lines of the subroutine itself.
Return function
Returns the Debugger to where a calling program resumes control. You use this
from within a subroutine call.
Slide
20 of
32
Breakpoints
static
Set directly into a program's code with the Editor. Static breakpoints are generally
user-independent. You can also set user-dependent static points.
dynamic
Set from within the ABAP/4 Editor or the Debugger. Dynamic breakpoints are
invisible when you display the program's code in the Editor
watchpoints
Set from within the Debugger. Watchpoints are field-specific. You use a
watchpoint to observe changes to a particular field. The Debugger interrupts the
program when the field's contents change.You can only set one watchpoint at a
time.
key word or event breakpoints
Set from within the Debugger. The Debugger interrupts the program when the
ABAP/4 runtime processor comes across the specified keyword or event in the
program's code.
Setting Breakpoints
o Line-oriented breakpoints
You can place a dynamic breakpoint at the current cursor position
by choosing the Choose function or double clicking. Lines where a
breakpoint has been set are marked with a STOP symbol. You can
display all of the breakpoints currently set by choosing Goto
Breakpoints. You can delete breakpoints by positioning the cursor
on one and double clicking or using the Breakpoint menu.
Dynamic breakpoints can be set or deleted without making edit
changes to your program.
You can position the cursor and then choose Utilities
Breakpoints Set.
You can use the BREAK-POINT statement in the ABAP/4 Editor.
This creates a static breakpoint that is removed only by editing
your program.
o Field content-oriented breakpoints
You can set a watchpoint by choosing Goto Fields. When the
field on which the watchpoint is set contains a different value, the
debugger stops.
You can set breakpoints dependent on SY-SUBRC <> 0 by
choosing Breakpoint Breakpoint at...
o Command-oriented breakpoints
You can set breakpoints for ABAP/4 keywords, events and subroutines by
choosing Breakpoint Breakpoint at...
Slide
22 of
32
Program Breakpoint
Breakpoints allow you to examine variable values and displayed results during
program execution.
Debugging Strategies
There are two debugging strategies from within the ABAP/4 Development
Workbench. You can set breakpoints in a program and then start the program
within the Debugger. Alternatively, you can run a program in the Debugger
without any breakpoints.
A breakpoint is a signal within a line of code that tells the ABAP/4 runtime
processor to interrupt a program at the line. Setting breakpoints is a good strategy
if you want to examine a program:
You can also run the Debugger without first setting breakpoints. For example, if
you want to examine a program from the first line of code. You can also use this
method with a transaction from a first Process Before Output (PBO) module call.
If you are debugging an unfamiliar program and you do not know where to set
breakpoints effectively, you might also debug a program without breakpoints.
Slide
23 of
32
The Display list button on the Application toolbar allows you to display the
screen on which a list is being created. This lets you see what has been written to
the screen when the breakpoint is reached in the programs execution. If the
breakpoint is used with a loop or select, then each time the breakpoint is reached,
the Display list button can be used to view the current status of the list that is
being written to the screen.
Breakpoint Table
From your program listing in the ABAP/4 Editor if you want to know where you have set
dynamic breakpoints, select Utilities Breakpoints Display to get a table which is
a listing that is an overview of all existing breakpoints in a program's code.
From the breakpoint table screen, you can navigate to or delete individual breakpoints.
Click the check box of the breakpoint, then click the Navigate button to go to that line in
the ABAP/4 Editor.
To delete a breakpoint from this table, click the check box of the desired breakpoint, then
click the Del. sel. objects button.
Slide
25 of
32
Slide
26 of
32
Slide
27 of
32
Slide
28 of
32