ABAP/4 Programming Lesson 3
Control Statements and Data Click here to start Table of Contents
Author: Roger Hayen
Control Statements and Data
Email:
[email protected]
Lesson 3 Topics
Home Page: Contents
Lesson Objectives Flow of Control Flow of Control Concept Control Commands: Overview Logical Expressions IF Statement CASE Statement DO Loop WHILE Loop CONTINUE and CHECK EXIT Statement Debugging Programs Branching to Debugging Mode Debugging Mode Debugger Views Initiate Debugging Mode
Important Debugging Functions Breakpoint Setting Breakpoints Program Breakpoint Display List at 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 This lesson consists of the following main topics: o o o
o o
Flow of Control Logical Expressions Conditional statements IF/ELSEIF/ELSE/ENDIF CASE/WHEN/ENDCASE DO/ENDDO WHILE/ENDWHILE CONTINUE, CHECK, EXIT Debugging programs using the ABAP/4 Debugger Using Breakpoints with programs
Slide 3 of 32
Lesson Objectives Upon completion of this lesson, you should be able to: o o o o o
Describe the use of the follow of control in ABAP/4 programs Write ABAP/4 program statements for conditional logic and loops Use the CONTINUE, CHECK, and EXIT statements with condition logic and loops Start the Debugging function Understand how to use Breakpoints in debugging programs
Slide 4 of 32
Flow of Control Like other higher programming languages, ABAP/4 provides standard keywords to control the flow of a program. These keywords are used for o o
branching (IF, CASE) looping (DO, WHILE)
However, ABAP/4 differs from these other programming languages in that it has internal control and external control of the program flow. o o
The internal control is steered by standard keywords as mentioned above. You define this in your program code. The external control is steered by events. Events are generated either from other ABAP/4 programs (system programs or user programs) or from interactive user input (like, for example, using the mouse to click on the screen). The system does not necessarily process the statements in the same sequence as they are listed in an ABAP/4 program. This makes
ABAP/4 an event-driven programming language and thus similar, for example, to Microsoft's Visual Basic.
Slide 5 of 32
Flow of Control Concept The connection between time events and an ABAP/4 program is provided by event keywords. Each statement in an ABAP/4 program belongs to a particular event keyword. Even if you do not specify any event keywords in your program, all statements automatically belong to the standard event keyword (START-OF-SELECTION). The order of the event statements in the program is completely irrelevant. All statements belonging to a particular event keyword form a processing block. A processing block is a module which is processed the moment the event occurs. The program flow within a processing block depends on the
internal control. The system processes the statements sequentially or as defined by the internal control standard keywords.
Suppose a report program which generates a list and provides some drilldown 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
Control Commands: Overview
ABAP/4 does not have a GOTO statement or equivalent. Control commands can be nested. if / elseif / else / endif: branch according to arbitrary conditions. case / when / endcase: branch according to a fixed set of possible values.
Slide 7 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 ....
... 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 and , you can use different logical operators. o o
Logical expressions can be linked with NOT, AND and OR. You can nest parenthetical expressions as deeply as you want. The parentheses which denote sub-expressions always count as one word. They must therefore be separated by spaces.
o
o
If you compare two type C fields with unequal length, the shorter field is lengthened to match the length of the longer one when the comparison is made. It is filled from the right-hand end with spaces. There is a whole range of further comparative operators which you can use to compare strings and bit comparisons.
For additional information see: Comparisons with All Field Types Comparisons with Character Strings and Numeric Strings Comparisons of Bit Structures Also, you can perform tests to check whether data fields fulfill certain criteria: Checking Whether a Field Belongs to a Range Checking for the Initial Value Checking Selection Criteria In addition, you can combine several logical expressions into one single logical expression: Combining Several Logical Expressions
Comparisons with All Field Types For comparisons with all field types, you can use the following operators in logical expressions: EQ = NE <> >< LT < LE <= GT >
Meaning equal to equal to not equal to not equal to not equal to less than less than less than or equal to less than or equal to greater than greater than
GE >=
greater than or equal to greater than or equal to
The operands can be database fields, internal fields, literals, or constants. Besides elementary fields, you can use structured data types as operands together with operators from the above table. Field strings are compared component by component and nested structures are broken down into elementary fields. 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: 1. If one of the operands is a floating point number (type F), the system also converts the other operands to type F. 2. If one of the operands is a packed field (type P), the system also converts the other operands to type P. 3. If one of the operands is a date field (type D) or a time field (type T), the system converts the other operands to type D or T. Comparisons between date and time fields are not supported and result in termination of the program. 4. If one of the operands is a character field (type C) and the other operand is a hexadecimal field (type X), the system converts the operand of type X to type C. 5. If one of the operands is a character field (type C) and the other a numeric field (type N), the system converts both operands to type P.
DATA:
F TYPE F VALUE '100.00', P TYPE P VALUE '50.00' DECIMALS 2, I TYPE I VALUE '30.00'. WRITE 'The following logical expressions are true:'. IF F >= P . WRITE: / F,'>=',P. ELSE. WRITE: / F,'<',P. ENDIF. IF I EQ P . WRITE: / I,'EQ',P. ELSE.
WRITE: / I,'NE',P. ENDIF. This produces the following output: The following logical expressions are true: 1.000E+02 >= 50.00 30 NE 50.00 Here, two logical expressions are used in IF statements. If a logical expression is true, it is displayed on the screen. If a logical expression is false, the inverse expression appears on the screen
Comparisons with Character Strings and Numeric Strings For comparisons with character strings (type C) and numeric text (type N), you can use the following operators in logical expressions
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
With comparisons containing one of these operations, the operands should be of type N or C because the system does not perform any other type conversion apart from type N to type C. The function of the operators is as follows: CO (Contains Only) The logical expression
CO is true if contains only characters from . The comparison is casesensitive. Trailing blanks are included. If the comparison is true, the system field SY-FDPOS contains the length of . If it is false, SY-FDPOS contains the offset of the first character of that does not occur in . CN (Contains Not only) The logical expression CN is true if does also contains characters other than those in . 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 that does not also occur in . If it is false, SY-FDPOS contains the length of . CA (Contains Any) The logical expression CA is true if contains at least one character from . The comparison is casesensitive. If the comparison is true, the system field SY-FDPOS contains the offset of the first character of that also occurs in . If it is false, SY-FDPOS contains the length of . NA (contains Not Any) The logical expression NA is true if does not contain any character from . The comparison is casesensitive. If the comparison is true, the system field SY-FDPOS contains the length of . If it is false, SY-FDPOS contains the offset of the first character of that occurs in . CS (Contains String) The logical expression
CS is true if contains the character string . Trailing blanks are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SYFDPOS contains the offset of in . If it is false, SY-FDPOS contains the length of . NS (contains No String) The logical expression NS is true if does not contain the character string . 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 . If it is false, SY-FDPOS contains the offset of in . CP (Contains Pattern) The logical expression CP is true if contains the pattern . If is of type C, you can use the following wildcards in : * for any character string + for any single character 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 in . If it is false, SY-FDPOS contains the length of . If you want to perform a comparison on a particular character in , place the escape character # in front of it. You can use the escape character # to specify • • • • •
characters in upper and lower case the wildcard character "*" (enter #*) the wildcard character "+" (enter #+) the escape symbol itself (enter ##) blanks at the end of a string (enter #___)
NP (contains No Pattern)
The logical expression NP is true if does not contain the pattern . In , 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 . If it is false, SY-FDPOS contains the offset of in .
DATA: F1(5) TYPE C VALUE , F2(5) TYPE C VALUE . IF F1 F2. WRITE: / 'Comparison true, SY-FDPOS=', SY-FDPOS. ELSE. WRITE: / 'Comparison false, SY-FDPOS=', SY-FDPOS. ENDIF. The following table shows the results of executing this program, depending on which operators and F1 / F2 fields you use.
'BD
'
CO
'BD
'
Result
SY-FDPOS
'ABCD '
true
5
CO
'ABCDE'
false
2
'ABC12'
CN
'ABCD '
true
3
'ABABC'
CN
'ABCD '
false
5
'ABcde'
CA
'Bd
true
1
'ABcde'
CA
'bD
'
false
5
'ABAB '
NA
'AB
'
false
0
'ababa'
NA
'AB
'
true
5
'ABcde'
CS
'bC
'
true
1
'ABcde'
CS
'ce
'
false
5
'ABcde'
NS
'bC
'
false
1
'
'ABcde'
NS
'ce
'ABcde'
CP
'ABcde'
'
true
5
'*b*'
true
1
CP
'*#b*'
false
5
'ABcde'
NP
'*b*'
false
1
'ABcde'
NP
'*#b*'
true
5
Comparisons of Bit Structures 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. Meaning O
bits are one
Z
bits are zero
M
bits are mixed
The second operand should have a length of one byte. It is not necessary, but advantageous, to use a hexadecimal field (type X) of length one for the second operand because it has a length of one byte and its numeric value is directly related to its bit structure. The function of the operators is as follows: O (bits are one) The logical expression O is true if the bit positions that are 1 in , are 1 in . Z (bits are zero) The logical expression
Z is true if the bit positions that are 1 in , are 0 in . M (bits are mixed) The logical expression M is true if from the bit positions that are 1 in , at least one is 1 and one is 0 in .
DATA: C VALUE 'C', HEXDEC TYPE X, I TYPE I. HEXDEC = 0. DO 256 TIMES. I = HEXDEC. IF C O HEXDEC. WRITE: / HEXDEC, I. ENDIF. HEXDEC = HEXDEC + 1. ENDDO. This produces the following output: 00 0 01 1 02 2 03 3 40 64 41 65 42 66 43 67 Here, the bit structure of the character 'C' is compared to all hexadecimal numbers HEXDEC between '0' and 'FF' (255 in the decimal system), using the operator O. The decimal value of HEXDEC, I is determined by using the automatic type conversion during the assignment of HEXDEC to I. If the comparison is true, the hexadecimal number and its decimal value are displayed on the screen. The following table shows the bit structure of these numbers.
hexadecimal decimal bit structure 00
0
00000000
01
1
00000001
02
2
00000010
03
3
00000011
40
64
01000000
41
65
01000001
42
66
01000010
43
67
01000011
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.
Checking Whether a Field Belongs to a Range To check whether a value belongs to a particular range, you use a logical expression with the BETWEEN parameter as follows: Syntax .... BETWEEN AND ..... This expression is true if occurs in the range between and . It is a short form of the following logical expression: IF GE AND LE .
DATA: NUMBER TYPE I, FLAG. .... NUMBER = ... .... IF NUMBER BETWEEN 3 AND 7. FLAG = 'X'. ELSE. FLAG = ' '.
ENDIF. Here, if the contents of NUMBER occur between 3 and 7, the field FLAG is set to "X".
Checking for the Initial Value To check whether a field is set to its initial value, you use a logical expression with the IS INITIAL parameter as follows. Syntax .... IS INITIAL ..... This expression is true if the field 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 statement is executed (see Resetting Values to Default Values).
DATA FLAG VALUE 'X'. IF FLAG IS INITIAL. WRITE / 'Flag is initial'. ELSE. WRITE / 'Flag is not initial'. ENDIF. CLEAR FLAG. IF FLAG IS INITIAL. WRITE / 'Flag is initial'. ELSE. WRITE / 'Flag is not initial'. ENDIF. This produces the following output: Flag is not initial Flag is initial. 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: •
•
•
To combine several logical expressions together in one single expression which is true only if all of the component expressions are true, link the expressions with AND. To combine several logical expressions together in one single expression which is true if at least one of the component expressions is true, link the expressions with OR. To invert the result of a logical expression, specify NOT in front of it.
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.
DATA: F TYPE F VALUE '100.00', N(3) TYPE N VALUE '123', C(3) TYPE C VALUE '456'. WRITE 'The following logical expression is true:'. IF ( C LT N ) AND ( N GT F ). WRITE: / '(',C,'lt',N,') AND (',N,'gt',F,')'. ELSE. WRITE: / '(',C,'ge',N,') OR (',N,'le',F,')'. ENDIF. This produces the following output: The following logical expression is true: ( 456 ge 123 ) OR ( 123 le 1.000000000000000E+02 )
In this example, a logical expressions is used in an IF statement. If the logical expression is true, it is displayed on the screen. If it is false, the inverted expression appears on the screen.
Slide 8 of 32
IF Statement For the IF Statement: o o o
The ELSE and ELSEIF statements are optional. If the logical expression is fulfilled, the following sequence of statements is executed. If the logical expression is not fulfilled, the ELSE or ELSEIF section is processed. If there is no ELSE or no further ELSEIF statement, the program continues after the ENDIF statement.
o
You can include any number of ELSEIF statements between IF and ENDIF. A maximum of one of the sequences of statements will be executed.
For additional information see: Conditional Branching using IF
Conditional Branching using IF The IF statement allows you to divert the program flow to a particular statement block, depending on a condition. This statement block consists of all the commands which occur between an IF statement and the next ELSEIF, ELSE, or ENDIF statement. Syntax IF . <statement block> ELSEIF . <statement block> ELSEIF . <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. To formulate conditions in IF or ELSEIF statements, you can use any logical expression described in Programming Logical Expressions . ABAP/4 allows unlimited nesting of IF - ENDIF statement blocks, but they must terminate within the same processing block. In other words, an IF - ENDIF block cannot contain an event keyword.
DATA: TEXT1(30) VALUE 'This is the first text', TEXT2(30) VALUE 'This is the second text', TEXT3(30) VALUE 'This is the third text', STRING(5) VALUE 'eco'. IF TEXT1 CS STRING. WRITE / 'Condition 1 is fulfilled'. ELSEIF TEXT2 CS STRING. WRITE / 'Condition 2 is fulfilled'. ELSEIF TEXT3 CS STRING. WRITE / 'Condition 3 is fulfilled'. ELSE. WRITE / 'No condition is fulfilled'. ENDIF. This produces the following output: Condition 2 is fulfilled. Here, the second logical expression TEXT2 CS STRING is true because the string "eco" occurs in TEXT2.
Slide 9 of 32
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. o o
Only one of the sequence of statements is executed. The WHEN OTHERS statement is optional.
DATA: TEXT1 VALUE 'X', TEXT2 VALUE 'Y',
TEXT3 VALUE 'Z', STRING VALUE 'A'. CASE STRING. WHEN TEXT1. WRITE: / 'String is', TEXT1. WHEN TEXT2. WRITE: / 'String is', TEXT2. WHEN TEXT3. WRITE: / 'String is', TEXT3. WHEN OTHERS. WRITE: / 'String is not', TEXT1, TEXT2, TEXT3. ENDCASE. This produces the following output: 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". For additional information see: Conditional Branching with CASE
Conditional Branching with CASE To execute different statement blocks depending on the contents of particular data fields, you use the CASE statement as follows: Syntax CASE . WHEN . <statement block> WHEN . <statement block> WHEN . <statement block> WHEN ... ...... WHEN OTHERS. <statement block> ENDCASE. 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. 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. The conditional branching using CASE is a shorter form of similar processing with IF: IF = . <statement block> ELSEIF = . <statement block> ELSEIF = . <statement block> ELSEIF = ... ... ELSE. <statement block> ENDIF. In ABAP/4, you can nest CASE - ENDCASE blocks and combine them with IF - ENDIF blocks, but they must terminate within the same processing block.
Slide 10 of 29
DO Loop The system continues processing the statement block introduced by DO and concluded by ENDDO until it finds an EXIT, STOP, or REJECT statement. You can limit the number of times the loop is processed with the TIMES option. can be literal or a variable. If 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 o
o
The TIMES parameter is optional. If you do not specify it, you need to build a termination condition into the loop (see EXIT statement). 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. Modifying the contents of SY-INDEX will NOT alter the number of loop passes within DO ... ENDDO. SY-INDEX is maintained by the ABAP
o
processor and is set to the appropriate value at the top of the DO … ENDDO loop. DO loops may be nested. SY-INDEX is associated only with the current nested looping construct.
For additional information see: Unconditional Looping using DO
Conditional Branching with CASE To execute different statement blocks depending on the contents of particular data fields, you use the CASE statement as follows: Syntax CASE . WHEN . <statement block> WHEN . <statement block> WHEN . <statement block> WHEN ... ...... WHEN OTHERS. <statement block> ENDCASE. 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. 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. The conditional branching using CASE is a shorter form of similar processing with IF: IF = . <statement block> ELSEIF = . <statement block> ELSEIF = . <statement block> ELSEIF = ...
... ELSE. <statement block> ENDIF. In ABAP/4, you can nest CASE - ENDCASE blocks and combine them with IF - ENDIF blocks, but they must terminate within the same processing block.
1: This example shows the basic form of a DO loop. DO. WRITE SY-INDEX. IF SY-INDEX = 3. EXIT. ENDIF. ENDDO. This produces the following output: 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. This produces the following output: 1 1 2 1
2
3
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 SYINDEX 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. , , ,... must be a series of equidistant fields of the same type and length in memory. In the first loop pass, is assigned to in the second loop pass is assigned to and so on. You can use several VARYING options in one DO statement. If you change the control variable inside the DO loop, the system changes the corresponding field automatically. Caution! Ensure that you do not process the loop more times than the number of variables , , ,... involved, since this may result in a runtime error. This example shows how VARYING options can be used in a DO loop. DATA: BEGIN OF TEXT, WORD1(4) VALUE 'This', WORD2(4) VALUE 'is', WORD3(4) VALUE 'a', WORD4(4) VALUE 'loop', END OF TEXT. DATA: STRING1(4), STRING2(4). DO 4 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD2. WRITE STRING1. IF STRING1 = 'is'. STRING1 = 'was'. ENDIF. ENDDO.
SKIP. DO 2 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD3 VARYING STRING2 FROM TEXT-WORD2 NEXT TEXT-WORD4. WRITE: STRING1, STRING2. ENDDO. This produces the following output: This is a loop This was a loop The field string TEXT represents a series of four equidistant fields in memory. Each time the first DO loop is processed, its components are assigned one by one to STRING1. If STRING1 contains "is", it is changed to "was" and TEXT-WORD2 is automatically changed to "was". Each time the second DO loop is processed, the components of TEXT are passed to STRING1 and to STRING2. For additional information see: Unconditional Looping using DO
Unconditional Looping using DO If you want to process a statement block more than once, you can program a loop with the DO statement as follows: Syntax DO [ TIMES] [VARYING FROM NEXT ]. <statement block> ENDDO. 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 To terminate the processing of a loop, use one of the following keywords. 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 . The CONTINUE, CHECK, and EXIT statements that work in DO and WHILE loops can be used as well in: •
LOOP - ENDLOOP loops, which are used to process internal tables(see Loop Processing).
•
SELECT - ENDSELECT loops, which are used to read data from database tables(see Selecting All Data from Several Lines).
Terminating a Loop Pass Unconditionally 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. This produces the following output:
1
3
4
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. can be literal or a variable. If 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.
Terminating a Loop Pass Conditionally To terminate a loop pass conditionally, use the CHECK statement as follows: Syntax CHECK . 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 , use any logical expression described in Programming Logical Expressions .
DO 4 TIMES. CHECK SY-INDEX BETWEEN 2 and 3. WRITE SY-INDEX. ENDDO. This produces the following output: 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. Provided the logical expression is fulfilled, the sequence of statements is executed. The system continues processing the statement block introduced by WHILE and concluded by ENDWHILE statements as long as <> is true or until the system finds an EXIT, STOP, or REJECT statement.
o o o
The system field SY-INDEX contains the number of times the loop has been processed. The number of loop passes cannot be altered via the SY-INDEX field within the WHILE ... ENDWHILE loop. 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. (For additional information see: Terminating Loops)
DATA: LENGTH TYPE I VALUE 0, STRL TYPE I VALUE 0, STRING(30) TYPE C VALUE 'Test String'. STRL = STRLEN( STRING ). WHILE STRING NE SPACE. WRITE STRING(1). LENGTH = SY-INDEX. SHIFT STRING. ENDWHILE. WRITE: / 'STRLEN: ', STRL. WRITE: / 'Length of string:', LENGTH. This produces the following output: TestString STRLEN: 11 Length of string: 11 Here, a WHILE loop is used to determine the length of a character string. This is done by shifting the string one position to the left each time the loop is processed until it contains only blanks. This example has been chosen to demonstrate the WHILE statement. However, the string length itself can be determined more easily and more efficiently by using the built-in function STRLEN, which is also shown in the example. For additional information see:
Conditional Loops using WHILE 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 as follows: Syntax WHILE [VARY FROM NEXT ]. <statement block> ENDWHILE. The system continues processing the statement block introduced by WHILE and concluded by ENDWHILE statements as long as is true or until the system finds an EXIT, STOP, or REJECT statement (see Terminating Loops). For , you can use any logical expression described in Programming Logical Expressions . 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 each time the loop is processed. , , ,... must be a series of equidistant fields of the same type and length in memory. In the first loop pass, is assigned to ; in the second loop pass is assigned to ; 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
CONTINUE and CHECK
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.
For additional information see: Terminating a Loop Pass Unconditionally
Terminating a Loop Pass Unconditionally 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. This produces the following output: 1
3
4
Here, the system terminates the second loop pass without processing the WRITE statement. Terminating a Loop Pass Conditionally
Terminating a Loop Pass Conditionally To terminate a loop pass conditionally, use the CHECK statement as follows: Syntax CHECK . 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 , use any logical expression described in Programming Logical Expressions .
DO 4 TIMES. CHECK SY-INDEX BETWEEN 2 and 3. WRITE SY-INDEX. ENDDO. This produces the following output: 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).
For additional information see: Termintating a Loop Entirely
Terminating a Loop Entirely To terminate a loop entirely without any condition, use the EXIT statement as follows: 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. This produces the following output: 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. Debugging activities include: · Switch on the Debugger. · Set and delete dynamic breakpoints. · Set watchpoints. · Stop a program at specific key words or events or when a fields contents change. · Continue processing after an interrupt. · Display field contents during runtime.
· Display the contents of an internal table. · Change field contents for testing purposes. · Change the contents of an internal table. · Display and use the Debugger's six different views.
Slide 15 of 29
Branching to Debugging Mode 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: O
Overview Displays the structure of the program being debugged. This view shows the events, subroutines, and modules contained
within a program and indicates which section is currently being processed.
S
Displays an active event chain and the call sequence up to the current breakpoint. The most recent active call appears at the Call Stack top of the list and the previous calls appear underneath. As soon as an event is over (such as when START-OF-SELECTION is complete), the event is removed from the stack display.
V
Variables
Displays the contents of up to four fields or field strings. This is the default view of the Debugger.
F
Field Display
Displays a field's contents and technical characteristics. Use this screen to define a watchpoint. A watchpoint sets an interrupt whenever the contents of the specified field change.
T
Table Display
Displays the contents of an internal table. Use this view to modify entries in the internal table.
P
Displays all programs needed to run the report or transaction Programs being debugged, including system programs. Use this view to get technical and administrative data about each program.
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
Initiate Debugging Mode 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'. To execute a program in debugging mode, do one of the following: In the ABAP/4 Editor initial screen: Choose Program Debugging or choose Debugging.
Execute
In the Object Browser: Select a report or transaction and choose Test/execute. When the system queries you for a mode, choose Debugging. From any screen: Choose System
Utilities
Debug ABAP/4
Slide 19 of 32
Important Debugging Functions 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 You can set the following types of 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. Dynamic breakpoints are user-specific. If you want to interrupt a program when you execute it but not when others are running it, then you should set dynamic breakpoints. Dynamic breakpoints are more flexible than static breakpoints, because they can be removed or deactivated during runtime. You can set dynamic breakpoints without making any changes to your program's code. For additional information on breakpoints see Help Basis System ABAP/4 Development Workbench ABAP/4 Workbench Tools Debugging Tools
Slide 21 of 32
Setting Breakpoints You set breakpoints as follows: o
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. 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... Command-oriented breakpoints
o
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: · after the system has already processed certain events · just before a specific event is carried out · by skipping quickly to specific routines or calls 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
Display List at Breakpoint 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. The Continue button on the Application toolbar is used to continue execution of the program once you are done examining the results displayed on the listing screen.
Slide 24 of 32
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