Professional Documents
Culture Documents
04-Control Techniques
04-Control Techniques
Objectives
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)
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).
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
OUTPUT:
The following logical expressions are true: 1.000000000000000E+02 >= 50.00 30 NE 50.00
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
'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
Programming Branches and Loops Branching Conditional branching using IF Conditional branching using CASE Unconditional looping using DO Conditional loops using WHILE Terminating Loops
Loops
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 <condition1>. <statement block> ELSEIF <condition2>. <statement block> ELSEIF <condition3>. <statement block> ..... ELSE. <statement block> ENDIF.
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.
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
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.
To terminate the processing of a loop, use one of the following keywords. Keyword CONTINUE CHECK EXIT Purpose Terminating a Loop Pass Unconditionally Terminating a Loop Pass Conditionally Terminating a Loop Entirely
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.
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 2 1 3 3 2 4 for CONTINUE statement for CHECK statement for EXIT statement