- The document discusses controlling the flow of ABAP/4 programs using control statements like IF, CASE, DO and WHILE for branching and looping.
- It describes logical expressions that can be used to compare data fields and character strings. Operators like EQ, NE, LT are used to compare field types, while CO, CN, CA are used to compare strings.
- Programming techniques like conditional branching with IF and CASE, unconditional and conditional looping with DO and WHILE are covered. Statements like CONTINUE, CHECK and EXIT can be used to terminate loops.
"SkyPat" is a C++ performance analyzing and testing framework on Android and Linux platforms. It combines perf_events and unit tests together and gives programmers the power of fine-grained performance analysis via writing typical test cases.
by Peter Chang
"SkyPat" is a C++ performance analyzing and testing framework on Android and Linux platforms. It combines perf_events and unit tests together and gives programmers the power of fine-grained performance analysis via writing typical test cases.
by Peter Chang
Functions allow structuring the programs in segments of code to perform individual tasks. The typical case for creating a function is when one needs to perform the same action multiple times in a program.
Standardizing code fragments into functions has several advantages −
Functions help the programmer stay organized. Often this helps to conceptualize the program.
Functions codify one action in one place so that the function only has to be thought about and debugged once.
This also reduces chances for errors in modification, if the code needs to be changed.
Functions make the whole sketch smaller and more compact because sections of code are reused many times.
They make it easier to reuse code in other programs by making it modular, and using functions often makes the code more readable.
Functions allow structuring the programs in segments of code to perform individual tasks. The typical case for creating a function is when one needs to perform the same action multiple times in a program.
Standardizing code fragments into functions has several advantages −
Functions help the programmer stay organized. Often this helps to conceptualize the program.
Functions codify one action in one place so that the function only has to be thought about and debugged once.
This also reduces chances for errors in modification, if the code needs to be changed.
Functions make the whole sketch smaller and more compact because sections of code are reused many times.
They make it easier to reuse code in other programs by making it modular, and using functions often makes the code more readable.
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressionsdubon07
This presentation contains the following topics:
1. Working with Elementary Data Objects.
2. Basic ABAP statements
3. Performing ABAP calculations
4. ABAP Calculator
5. ABAP Structures
6. How to create ABAP structures
7. Dialog Messages
8. ABAP IF statement and CASE
9. ABAP Logical Expressions
10. ABAP Loops
11. ABAP Search Helps F4
12. ABAP Elementary Search Help
13. ABAP Exercise: Flight Information Report Summary
2. 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
3. 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
4. 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
5. The general structure of ABAP/4 programs (report ) as follows
ABAP/4 CONTROLLING TECHNIQUES
6. 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
7. 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
8. 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
9. OUTPUT:
The following logical expressions are true:
1.000000000000000E+02 >= 50.00
30 NE 50.00
ABAP/4 CONTROLLING TECHNIQUES
10. 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
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
Programming Branches 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
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.
15. 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.
16. 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.
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
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.
21. 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.
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
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
24. 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
25. 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.
26. 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.
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.
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.
30. 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'.
31. 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.
32. 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
33. 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.
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
*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.