Control TechniquesControl Techniques
Objectives
In this Chapter you will learn...
• To Code common control statements IF, CASE, DO,
WHILE for branching as well as looping.
• Control the program sequence using CONTINUE, CHECK
and EXIT.
ABAP/4 CONTROLLING TECHNIQUES
Controlling the Flow of ABAP/4 Programming
The flow of an ABAP/4 program can be controlled internally
and externally.
Internal control is steered by using some standard
control keywords( IF,CASE, DO,WHILE).
These keywords are used for
• branching (IF, CASE)
• looping (DO, WHILE)
ABAP/4 CONTROLLING TECHNIQUES
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).
ABAP/4 CONTROLLING TECHNIQUES
The general structure of ABAP/4 programs (report ) as follows
ABAP/4 CONTROLLING TECHNIQUES
Programming Logical Expressions
Use logical expressions in conditions statements with key
words
IF, CHECK and WHILE to compare data fields.
• comparisons with all field types
• comparisons with character strings and numeric strings
ABAP/4 CONTROLLING TECHNIQUES
Comparisons with all field types:
Use following operators in logical expressions for comparisons
with all field types
• EQ(=)
• NE(<> or ><)
• LT(<)
• LE(<=>)
• GT(>)
• GE(>=)
ABAP/4 CONTROLLING TECHNIQUES
EXAMPLE:
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.
ABAP/4 CONTROLLING TECHNIQUES
OUTPUT:
The following logical expressions are true:
1.000000000000000E+02 >= 50.00
30 NE 50.00
ABAP/4 CONTROLLING TECHNIQUES
ABAP/4 CONTROLLING TECHNIQUES
Comparisons with character strings and Numeric strings
The following operators are used in logical expressions
CO ------Contains only
CN-------Contains not only
CA-------Contains Any
NA-------Contains not any
CS-------Contains String
NS-------Contains Not string
CP-------Contains pattern
NP-------Contains no pattern
ABAP/4 CONTROLLING TECHNIQUES
'AABB' co 'AB' True
'ABCD' co 'ABC' False
'AABB' cn 'AB' False
'ABCD' cn 'ABC' True
'AXCZ' ca 'AB' True
'ABCD' ca 'XYZ' False
'AXCZ' na 'ABC' False
'ABCD' na 'XYZ' True
ABAP/4 CONTROLLING TECHNIQUES
Programming Branches and Loops
Branching
• Conditional branching using IF
• Conditional branching using CASE
Loops
• Unconditional looping using DO
• Conditional loops using WHILE
• Terminating Loops
ABAP/4 CONTROLLING TECHNIQUES
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.
ABAP/4 CONTROLLING TECHNIQUES
Syntax
IF <condition1>.
<statement block>
ELSEIF <condition2>.
<statement block>
ELSEIF <condition3>.
<statement block>
.....
ELSE.
<statement block>
ENDIF.
ABAP/4 CONTROLLING TECHNIQUES
Example for Terminating a Loop Entirely
data: text1(30) value ‘this is the first text’,
text2(30) value ‘this is the second text2’,
text3(30) value ‘this is the third text3’.
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’.
output:
Condition 2 is fulfilled.
ABAP/4 CONTROLLING TECHNIQUES
Conditional Branching with CASE
To execute different statement blocks depending on the
contents of particular data fields.
Note: Conditional branching using CASE is shorter form of
similar processing with IF.
ABAP/4 CONTROLLING TECHNIQUES
Syntax
CASE <f>.
WHEN <f1>.
<statement block>
WHEN <f2>.
<statement block>
WHEN <f3>.
<statement block>
WHEN ...
......
WHEN OTHERS.
<statement block>
ENDCASE.
ABAP/4 CONTROLLING TECHNIQUES
Example:
Data: text1 value ‘x’,
text1 value ‘y’,
text1 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.
Output:
String is not x y z
ABAP/4 CONTROLLING TECHNIQUES
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 [<n> TIMES].
<statement block>
ENDDO.
Note: Avoid endless loops when working with do statement.If
you don’t use the times option, include at least EXIT,STOP or
REJECT.
ABAP/4 CONTROLLING TECHNIQUES
DO
Write sy-index.
If sy-index = 3.
Exit.
Endif.
ENDDO.
Output:
1 2 3
ABAP/4 CONTROLLING TECHNIQUES
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 <condition> .
<statement block>
ENDWHILE.
ABAP/4 CONTROLLING TECHNIQUES
Example:
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.
Out put.
Test string
strlen: 11
length of string: 11
ABAP/4 CONTROLLING TECHNIQUES
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
ABAP/4 CONTROLLING TECHNIQUES
Example for Terminating a Loop Pass Unconditionally
To terminate a loop pass immediately without any condition,
use the CONTINUE statement as follows:
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
ABAP/4 CONTROLLING TECHNIQUES
Example For Terminating a Loop Pass Conditionally:
To terminate a loop pass conditionally, use the CHECK
statement as follows:
Syntax
CHECK <condition>.
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.
ABAP/4 CONTROLLING TECHNIQUES
Example For Terminating a Loop Entirely:
To terminate a loop entirely without any condition, use the EXIT
statement as follows:
Syntax
EXIT.
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.
ABAP/4 CONTROLLING TECHNIQUES
SUMMARY
• Comparison statements are IF and CASE.
• Comparison operators like EQ,NE,LT,LE,GT,GE are used
for comparing all field types.
• Special operators like CO,CN.CA,NA,CS,NS,CP and NP are
used
for comparing strings
• Conditional branching using CASE is shorter form of similar
processing with IF
• The loop statements are do and while.
• sy-index always contains the counter for the current loop
pass. After the loop is finished, its value is reset to the value
it had when the loop began. Although you can change sy-
index, its value is reset with the next pass of the loop.
ABAP/4 CONTROLLING TECHNIQUES
SUMMARY
• Use the exit, continue, and check statements to modify
loop processing.
• exit terminates loop processing and continues execution
at the first statement following the loop.
• continue jumps to the end of the loop immediately
• check exp jumps to the end of the loop if exp is false.
When exp is true, check does nothing.
• DON'T use check or continue within a select loop to filter
out records. Instead, use the where clause to filter them
out.
ABAP/4 CONTROLLING TECHNIQUES
Ex-1.
Name of your report: ZABCD22111
Write a small report using CASE AND ENDCASE for the
comparison
of logical expression with the out put “ String is not X Y Z”.
Use TEXT1, TEXT2,TEXT3 and STRING with values ‘X’, ‘Y’, ‘Z’
and ‘A’
as data declaration part.
ABAP/4 CONTROLLING TECHNIQUES
SOLUTION
REPORT ZABCD22111 .
* Data declaration.
DATA: TEXT1 TYPE C VALUE 'X',
TEXT2 TYPE C VALUE 'Y',
TEXT3 TYPE C VALUE 'Z',
STRING TYPE C VALUE 'A'.
ABAP/4 CONTROLLING TECHNIQUES
*Logic used for case statement
uline.
write 'The CASE controll output is: '.
skip.
CASE STRING.
WHEN TEXT4 OR TEXT5.
WRITE: / 'String is', TEXT1, 'OR', TEXT2.
WHEN TEXT6.
WRITE: / 'String is', TEXT3.
WHEN OTHERS.
WRITE: / 'String is not', TEXT1, TEXT2, TEXT3.
ENDCASE.
ABAP/4 CONTROLLING TECHNIQUES
Ex-2.
Name of your report: ZABCD33111
Write a small report using DO loop with EXIT,CHECK and
CONTINUE
statement.use SY-INDEX for writing output.
The out put should be as follows.
1 3 4 for CONTINUE statement
2 3 for CHECK statement
1 2 for EXIT statement
ABAP/4 CONTROLLING TECHNIQUES
REPORT ZABCD33111 .
*Terminating loop using CONTINUE key word
write 'The output for terminating loop with CONTINUE key word
is: '.
skip.
DO 4 TIMES.
IF SY-INDEX = 2.
CONTINUE.
ENDIF.
WRITE SY-INDEX.
ENDDO.
SKIP.
ULINE.
ABAP/4 CONTROLLING TECHNIQUES
* *Terminating loop using CHECK key word
write 'The output for terminating loop with CHECK key word is: '.
SKIP.
DO 4 TIMES.
CHECK SY-INDEX BETWEEN 2 AND 3.
WRITE SY-INDEX.
ENDDO.
SKIP.
ULINE.
ABAP/4 CONTROLLING TECHNIQUES
*Terminating loop using EXIT key word
write 'The output for terminating loop with EXIT key word is: '.
SKIP.
DO 4 TIMES.
IF SY-INDEX = 3.
EXIT.
ENDIF.
WRITE SY-INDEX.
ENDDO.

control techniques

  • 1.
  • 2.
    Objectives In this Chapteryou will learn... • To Code common control statements IF, CASE, DO, WHILE for branching as well as looping. • Control the program sequence using CONTINUE, CHECK and EXIT. ABAP/4 CONTROLLING TECHNIQUES
  • 3.
    Controlling the Flowof ABAP/4 Programming The flow of an ABAP/4 program can be controlled internally and externally. Internal control is steered by using some standard control keywords( IF,CASE, DO,WHILE). These keywords are used for • branching (IF, CASE) • looping (DO, WHILE) ABAP/4 CONTROLLING TECHNIQUES
  • 4.
    External control issteered 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). ABAP/4 CONTROLLING TECHNIQUES
  • 5.
    The general structureof ABAP/4 programs (report ) as follows ABAP/4 CONTROLLING TECHNIQUES
  • 6.
    Programming Logical Expressions Uselogical expressions in conditions statements with key words IF, CHECK and WHILE to compare data fields. • comparisons with all field types • comparisons with character strings and numeric strings ABAP/4 CONTROLLING TECHNIQUES
  • 7.
    Comparisons with allfield types: Use following operators in logical expressions for comparisons with all field types • EQ(=) • NE(<> or ><) • LT(<) • LE(<=>) • GT(>) • GE(>=) ABAP/4 CONTROLLING TECHNIQUES
  • 8.
    EXAMPLE: DATA: F typef 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. ABAP/4 CONTROLLING TECHNIQUES
  • 9.
    OUTPUT: The following logicalexpressions are true: 1.000000000000000E+02 >= 50.00 30 NE 50.00 ABAP/4 CONTROLLING TECHNIQUES
  • 10.
    ABAP/4 CONTROLLING TECHNIQUES Comparisonswith character strings and Numeric strings The following operators are used in logical expressions CO ------Contains only CN-------Contains not only CA-------Contains Any NA-------Contains not any CS-------Contains String NS-------Contains Not string CP-------Contains pattern NP-------Contains no pattern
  • 11.
    ABAP/4 CONTROLLING TECHNIQUES 'AABB'co 'AB' True 'ABCD' co 'ABC' False 'AABB' cn 'AB' False 'ABCD' cn 'ABC' True 'AXCZ' ca 'AB' True 'ABCD' ca 'XYZ' False 'AXCZ' na 'ABC' False 'ABCD' na 'XYZ' True
  • 12.
    ABAP/4 CONTROLLING TECHNIQUES ProgrammingBranches and Loops Branching • Conditional branching using IF • Conditional branching using CASE Loops • Unconditional looping using DO • Conditional loops using WHILE • Terminating Loops
  • 13.
    ABAP/4 CONTROLLING TECHNIQUES ConditionalBranching 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.
  • 14.
    ABAP/4 CONTROLLING TECHNIQUES Syntax IF<condition1>. <statement block> ELSEIF <condition2>. <statement block> ELSEIF <condition3>. <statement block> ..... ELSE. <statement block> ENDIF.
  • 15.
    ABAP/4 CONTROLLING TECHNIQUES Examplefor Terminating a Loop Entirely data: text1(30) value ‘this is the first text’, text2(30) value ‘this is the second text2’, text3(30) value ‘this is the third text3’. 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’. output: Condition 2 is fulfilled.
  • 16.
    ABAP/4 CONTROLLING TECHNIQUES ConditionalBranching with CASE To execute different statement blocks depending on the contents of particular data fields. Note: Conditional branching using CASE is shorter form of similar processing with IF.
  • 17.
    ABAP/4 CONTROLLING TECHNIQUES Syntax CASE<f>. WHEN <f1>. <statement block> WHEN <f2>. <statement block> WHEN <f3>. <statement block> WHEN ... ...... WHEN OTHERS. <statement block> ENDCASE.
  • 18.
    ABAP/4 CONTROLLING TECHNIQUES Example: Data:text1 value ‘x’, text1 value ‘y’, text1 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. Output: String is not x y z
  • 19.
    ABAP/4 CONTROLLING TECHNIQUES UnconditionalLooping 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 [<n> TIMES]. <statement block> ENDDO. Note: Avoid endless loops when working with do statement.If you don’t use the times option, include at least EXIT,STOP or REJECT.
  • 20.
    ABAP/4 CONTROLLING TECHNIQUES DO Writesy-index. If sy-index = 3. Exit. Endif. ENDDO. Output: 1 2 3
  • 21.
    ABAP/4 CONTROLLING TECHNIQUES ConditionalLoops 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 <condition> . <statement block> ENDWHILE.
  • 22.
    ABAP/4 CONTROLLING TECHNIQUES Example: 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. Out put. Test string strlen: 11 length of string: 11
  • 23.
    ABAP/4 CONTROLLING TECHNIQUES Toterminate 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
  • 24.
    ABAP/4 CONTROLLING TECHNIQUES Examplefor Terminating a Loop Pass Unconditionally To terminate a loop pass immediately without any condition, use the CONTINUE statement as follows: 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
  • 25.
    ABAP/4 CONTROLLING TECHNIQUES ExampleFor Terminating a Loop Pass Conditionally: To terminate a loop pass conditionally, use the CHECK statement as follows: Syntax CHECK <condition>. 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.
  • 26.
    ABAP/4 CONTROLLING TECHNIQUES ExampleFor Terminating a Loop Entirely: To terminate a loop entirely without any condition, use the EXIT statement as follows: Syntax EXIT. 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.
  • 27.
    ABAP/4 CONTROLLING TECHNIQUES SUMMARY •Comparison statements are IF and CASE. • Comparison operators like EQ,NE,LT,LE,GT,GE are used for comparing all field types. • Special operators like CO,CN.CA,NA,CS,NS,CP and NP are used for comparing strings • Conditional branching using CASE is shorter form of similar processing with IF • The loop statements are do and while. • sy-index always contains the counter for the current loop pass. After the loop is finished, its value is reset to the value it had when the loop began. Although you can change sy- index, its value is reset with the next pass of the loop.
  • 28.
    ABAP/4 CONTROLLING TECHNIQUES SUMMARY •Use the exit, continue, and check statements to modify loop processing. • exit terminates loop processing and continues execution at the first statement following the loop. • continue jumps to the end of the loop immediately • check exp jumps to the end of the loop if exp is false. When exp is true, check does nothing. • DON'T use check or continue within a select loop to filter out records. Instead, use the where clause to filter them out.
  • 29.
    ABAP/4 CONTROLLING TECHNIQUES Ex-1. Nameof your report: ZABCD22111 Write a small report using CASE AND ENDCASE for the comparison of logical expression with the out put “ String is not X Y Z”. Use TEXT1, TEXT2,TEXT3 and STRING with values ‘X’, ‘Y’, ‘Z’ and ‘A’ as data declaration part.
  • 30.
    ABAP/4 CONTROLLING TECHNIQUES SOLUTION REPORTZABCD22111 . * Data declaration. DATA: TEXT1 TYPE C VALUE 'X', TEXT2 TYPE C VALUE 'Y', TEXT3 TYPE C VALUE 'Z', STRING TYPE C VALUE 'A'.
  • 31.
    ABAP/4 CONTROLLING TECHNIQUES *Logicused for case statement uline. write 'The CASE controll output is: '. skip. CASE STRING. WHEN TEXT4 OR TEXT5. WRITE: / 'String is', TEXT1, 'OR', TEXT2. WHEN TEXT6. WRITE: / 'String is', TEXT3. WHEN OTHERS. WRITE: / 'String is not', TEXT1, TEXT2, TEXT3. ENDCASE.
  • 32.
    ABAP/4 CONTROLLING TECHNIQUES Ex-2. Nameof your report: ZABCD33111 Write a small report using DO loop with EXIT,CHECK and CONTINUE statement.use SY-INDEX for writing output. The out put should be as follows. 1 3 4 for CONTINUE statement 2 3 for CHECK statement 1 2 for EXIT statement
  • 33.
    ABAP/4 CONTROLLING TECHNIQUES REPORTZABCD33111 . *Terminating loop using CONTINUE key word write 'The output for terminating loop with CONTINUE key word is: '. skip. DO 4 TIMES. IF SY-INDEX = 2. CONTINUE. ENDIF. WRITE SY-INDEX. ENDDO. SKIP. ULINE.
  • 34.
    ABAP/4 CONTROLLING TECHNIQUES **Terminating loop using CHECK key word write 'The output for terminating loop with CHECK key word is: '. SKIP. DO 4 TIMES. CHECK SY-INDEX BETWEEN 2 AND 3. WRITE SY-INDEX. ENDDO. SKIP. ULINE.
  • 35.
    ABAP/4 CONTROLLING TECHNIQUES *Terminatingloop using EXIT key word write 'The output for terminating loop with EXIT key word is: '. SKIP. DO 4 TIMES. IF SY-INDEX = 3. EXIT. ENDIF. WRITE SY-INDEX. ENDDO.