SlideShare a Scribd company logo
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.

More Related Content

Viewers also liked

Chapter 05 adding structures1
Chapter 05 adding structures1Chapter 05 adding structures1
Chapter 05 adding structures1Kranthi Kumar
 
Lecture02 abap on line
Lecture02 abap on lineLecture02 abap on line
Lecture02 abap on lineMilind Patil
 
Chapter 02 abap dictionary objects1
Chapter 02 abap dictionary objects1Chapter 02 abap dictionary objects1
Chapter 02 abap dictionary objects1Kranthi Kumar
 
Chapter 07 abap dictionary changes1
Chapter 07 abap dictionary changes1Chapter 07 abap dictionary changes1
Chapter 07 abap dictionary changes1Kranthi Kumar
 
Chapter 08 abap dictionary objects views1
Chapter 08 abap dictionary objects views1Chapter 08 abap dictionary objects views1
Chapter 08 abap dictionary objects views1Kranthi Kumar
 
Chapter 10 online help & documentation1
Chapter 10 online help & documentation1Chapter 10 online help & documentation1
Chapter 10 online help & documentation1Kranthi Kumar
 
0106 debugging
0106 debugging0106 debugging
0106 debuggingvkyecc1
 
Sap hr abap_course_content
Sap hr abap_course_contentSap hr abap_course_content
Sap hr abap_course_contentsap Logic
 
Abap slide exceptionshandling
Abap slide exceptionshandlingAbap slide exceptionshandling
Abap slide exceptionshandlingMilind Patil
 
0104 abap dictionary
0104 abap dictionary0104 abap dictionary
0104 abap dictionary
vkyecc1
 
Sujith ~ cross applications
Sujith ~ cross applicationsSujith ~ cross applications
Sujith ~ cross applicationsKranthi Kumar
 
Bapi jco[1]
Bapi jco[1]Bapi jco[1]
Bapi jco[1]
mateenjambagi
 
Chapter 03 foreign key relationships1
Chapter 03 foreign key relationships1Chapter 03 foreign key relationships1
Chapter 03 foreign key relationships1Kranthi Kumar
 
Abap slide lock Enqueue data clusters auth checks
Abap slide lock Enqueue data clusters auth checksAbap slide lock Enqueue data clusters auth checks
Abap slide lock Enqueue data clusters auth checksMilind Patil
 

Viewers also liked (16)

Chapter 05 adding structures1
Chapter 05 adding structures1Chapter 05 adding structures1
Chapter 05 adding structures1
 
Lecture02 abap on line
Lecture02 abap on lineLecture02 abap on line
Lecture02 abap on line
 
cardinality1
cardinality1cardinality1
cardinality1
 
Chapter 02 abap dictionary objects1
Chapter 02 abap dictionary objects1Chapter 02 abap dictionary objects1
Chapter 02 abap dictionary objects1
 
Chapter 07 abap dictionary changes1
Chapter 07 abap dictionary changes1Chapter 07 abap dictionary changes1
Chapter 07 abap dictionary changes1
 
Chapter 08 abap dictionary objects views1
Chapter 08 abap dictionary objects views1Chapter 08 abap dictionary objects views1
Chapter 08 abap dictionary objects views1
 
Chapter 10 online help & documentation1
Chapter 10 online help & documentation1Chapter 10 online help & documentation1
Chapter 10 online help & documentation1
 
0106 debugging
0106 debugging0106 debugging
0106 debugging
 
Sap hr abap_course_content
Sap hr abap_course_contentSap hr abap_course_content
Sap hr abap_course_content
 
Abap slide exceptionshandling
Abap slide exceptionshandlingAbap slide exceptionshandling
Abap slide exceptionshandling
 
0104 abap dictionary
0104 abap dictionary0104 abap dictionary
0104 abap dictionary
 
Sujith ~ cross applications
Sujith ~ cross applicationsSujith ~ cross applications
Sujith ~ cross applications
 
Bapi jco[1]
Bapi jco[1]Bapi jco[1]
Bapi jco[1]
 
Chapter 03 foreign key relationships1
Chapter 03 foreign key relationships1Chapter 03 foreign key relationships1
Chapter 03 foreign key relationships1
 
VIEWS
VIEWSVIEWS
VIEWS
 
Abap slide lock Enqueue data clusters auth checks
Abap slide lock Enqueue data clusters auth checksAbap slide lock Enqueue data clusters auth checks
Abap slide lock Enqueue data clusters auth checks
 

Similar to control techniques

Lecture05 abap on line
Lecture05 abap on lineLecture05 abap on line
Lecture05 abap on lineMilind Patil
 
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
ASHABOOPATHY
 
Introduction to AWK utility on unix.pptx
Introduction to AWK utility on unix.pptxIntroduction to AWK utility on unix.pptx
Introduction to AWK utility on unix.pptx
abuadu
 
Mod.2.pptx
Mod.2.pptxMod.2.pptx
Mod.2.pptx
Kokilak27
 
Arduino Functions
Arduino FunctionsArduino Functions
Arduino Functions
mahalakshmimalini
 
While loop and for loop 06 (js)
While loop and for loop 06 (js)While loop and for loop 06 (js)
While loop and for loop 06 (js)
AbhishekMondal42
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptx
AFANJIPHILL
 
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical ExpressionsUnit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
dubon07
 
PLSQL Practices
PLSQL PracticesPLSQL Practices
PLSQL Practices
Quang Minh Đoàn
 
PLSQL Advanced
PLSQL AdvancedPLSQL Advanced
PLSQL Advanced
Quang Minh Đoàn
 
Verilog Lecture2 thhts
Verilog Lecture2 thhtsVerilog Lecture2 thhts
Verilog Lecture2 thhts
Béo Tú
 
DB2 Systems Programming Tools of the Trade NA07B03
DB2 Systems Programming Tools of the Trade NA07B03DB2 Systems Programming Tools of the Trade NA07B03
DB2 Systems Programming Tools of the Trade NA07B03Linda Hagedorn
 
4. plsql
4. plsql4. plsql
4. plsql
Amrit Kaur
 
Software testing methodolgy with the control flow analysis
Software testing methodolgy with the control flow analysisSoftware testing methodolgy with the control flow analysis
Software testing methodolgy with the control flow analysis
RQK Khan
 
OpenPOWER Application Optimization
OpenPOWER Application Optimization OpenPOWER Application Optimization
OpenPOWER Application Optimization
Ganesan Narayanasamy
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
Dhilip Prakash
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
Dhilip Prakash
 

Similar to control techniques (20)

Lecture05 abap on line
Lecture05 abap on lineLecture05 abap on line
Lecture05 abap on line
 
ORACLE PL/SQL
ORACLE PL/SQLORACLE PL/SQL
ORACLE PL/SQL
 
Introduction to AWK utility on unix.pptx
Introduction to AWK utility on unix.pptxIntroduction to AWK utility on unix.pptx
Introduction to AWK utility on unix.pptx
 
Mod.2.pptx
Mod.2.pptxMod.2.pptx
Mod.2.pptx
 
Arduino Functions
Arduino FunctionsArduino Functions
Arduino Functions
 
pm1
pm1pm1
pm1
 
While loop and for loop 06 (js)
While loop and for loop 06 (js)While loop and for loop 06 (js)
While loop and for loop 06 (js)
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptx
 
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical ExpressionsUnit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
 
PLSQL Practices
PLSQL PracticesPLSQL Practices
PLSQL Practices
 
PLSQL Advanced
PLSQL AdvancedPLSQL Advanced
PLSQL Advanced
 
Verilog Lecture2 thhts
Verilog Lecture2 thhtsVerilog Lecture2 thhts
Verilog Lecture2 thhts
 
DB2 Systems Programming Tools of the Trade NA07B03
DB2 Systems Programming Tools of the Trade NA07B03DB2 Systems Programming Tools of the Trade NA07B03
DB2 Systems Programming Tools of the Trade NA07B03
 
4. plsql
4. plsql4. plsql
4. plsql
 
Software testing methodolgy with the control flow analysis
Software testing methodolgy with the control flow analysisSoftware testing methodolgy with the control flow analysis
Software testing methodolgy with the control flow analysis
 
Csd01
Csd01Csd01
Csd01
 
OpenPOWER Application Optimization
OpenPOWER Application Optimization OpenPOWER Application Optimization
OpenPOWER Application Optimization
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 

More from Kranthi Kumar

Creating simple comp
Creating simple compCreating simple comp
Creating simple compKranthi Kumar
 
Controllers and context programming
Controllers and context programmingControllers and context programming
Controllers and context programmingKranthi Kumar
 
Binding,context mapping,navigation exercise
Binding,context mapping,navigation exerciseBinding,context mapping,navigation exercise
Binding,context mapping,navigation exerciseKranthi Kumar
 
Web(abap introduction)
Web(abap introduction)Web(abap introduction)
Web(abap introduction)Kranthi Kumar
 
Chapter 07 debugging sap scripts
Chapter 07 debugging sap scriptsChapter 07 debugging sap scripts
Chapter 07 debugging sap scriptsKranthi Kumar
 
Chapter 06 printing sap script forms
Chapter 06 printing sap script formsChapter 06 printing sap script forms
Chapter 06 printing sap script formsKranthi Kumar
 
Chapter 05 sap script - configuration
Chapter 05 sap script - configurationChapter 05 sap script - configuration
Chapter 05 sap script - configurationKranthi Kumar
 
Chapter 04 sap script - output program
Chapter 04 sap script - output programChapter 04 sap script - output program
Chapter 04 sap script - output programKranthi Kumar
 
Chapter 02 sap script forms
Chapter 02 sap script formsChapter 02 sap script forms
Chapter 02 sap script formsKranthi Kumar
 

More from Kranthi Kumar (20)

Exercise in alv
Exercise in alvExercise in alv
Exercise in alv
 
Dynamic binding
Dynamic bindingDynamic binding
Dynamic binding
 
Data binding
Data bindingData binding
Data binding
 
Creating simple comp
Creating simple compCreating simple comp
Creating simple comp
 
Creating messages
Creating messagesCreating messages
Creating messages
 
Creating a comp
Creating a compCreating a comp
Creating a comp
 
Controllers and context programming
Controllers and context programmingControllers and context programming
Controllers and context programming
 
Context at design
Context at designContext at design
Context at design
 
Binding,context mapping,navigation exercise
Binding,context mapping,navigation exerciseBinding,context mapping,navigation exercise
Binding,context mapping,navigation exercise
 
Alv for web
Alv for webAlv for web
Alv for web
 
Web(abap introduction)
Web(abap introduction)Web(abap introduction)
Web(abap introduction)
 
Abap faq
Abap faqAbap faq
Abap faq
 
Sap abap material
Sap abap materialSap abap material
Sap abap material
 
Crm technical
Crm technicalCrm technical
Crm technical
 
Chapter 07 debugging sap scripts
Chapter 07 debugging sap scriptsChapter 07 debugging sap scripts
Chapter 07 debugging sap scripts
 
Chapter 06 printing sap script forms
Chapter 06 printing sap script formsChapter 06 printing sap script forms
Chapter 06 printing sap script forms
 
Chapter 05 sap script - configuration
Chapter 05 sap script - configurationChapter 05 sap script - configuration
Chapter 05 sap script - configuration
 
Chapter 04 sap script - output program
Chapter 04 sap script - output programChapter 04 sap script - output program
Chapter 04 sap script - output program
 
Chapter 02 sap script forms
Chapter 02 sap script formsChapter 02 sap script forms
Chapter 02 sap script forms
 
sap script overview
sap script overviewsap script overview
sap script overview
 

control techniques

  • 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.
  • 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 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.
  • 20. ABAP/4 CONTROLLING TECHNIQUES DO Write sy-index. If sy-index = 3. Exit. Endif. ENDDO. Output: 1 2 3
  • 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.