You are on page 1of 35

Control Techniques

ABAP/4 CONTROLLING 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

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 Unconditional looping using DO Conditional loops using WHILE Terminating Loops

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 dont 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 CONTINUE CHECK EXIT Purpose Terminating a Loop Pass Unconditionally Terminating a Loop Pass Conditionally 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 SYINDEX 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 syindex, 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 2 1 3 3 2 4 for CONTINUE statement for CHECK statement 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.

You might also like