Monday, June 11, 2012

0 Control Structures In ABAP


control structures :
This are used to control the flow of execution of the program
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).
Control structures are 2 types
1.Branching  Control structures(IF,CASE)
2.Looping Control structures(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).
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
Comparisons with all field types:
Use following operators in logical expressions for comparisons
with all field types
         EQ(=)
         NE(<> or ><)
         LT(<)
         LE(<=>)
         GT(>)
         GE(>=)
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.

Programming Branches and Loops
Branching
         Conditional branching using IF
         Conditional branching using CASE
Loops
         Unconditional looping using DO
         Conditional loops using WHILE
          Terminating Loops
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.
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 <field>.
   WHEN <value1>.
        <statement block>
   WHEN <value2>.
        <statement block>
   WHEN <value3>.
        <statement block>
   WHEN ...
   ......
   WHEN OTHERS.
        <statement block>
ENDCASE.
EX:
*//variable declaration
Data: text1 value 'x',
           text2 
value 'y',
           text3 
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,X
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.
Ex:
        DO.
  
Write:  sy-index.
  
If sy-index = 3.
       
Exit.
  
Endif.
ENDDO.
Output:  u will get the output as 1 2 3

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                                                   Purpose
CONTINUE                                     Terminating a Loop Pass Unconditionally
CHECK                                           Terminating a Loop Pass Conditionally
EXIT                                                Terminating a Loop Entirely
STOP                                              Terminates the Loop(used only in main program)
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.
Output: we will get the output as 1 3 4
Note: Here, the system terminates the second loop pass without processing the WRITE statement
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.
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.
Example For Terminating a Loop Entirely:
To terminate a loop entirely without any condition, use the EXIT statement as follows:
Syntax : EXIT.
Ex: DO 10 TIMES.
    
if sy-index = 4 .
      
exit.
      
ENDIF.
   
WRITE SY-INDEX.
ENDDO.
Output: 1 2 3
Here, the system terminates the entire loop processing in the third loop pass without processing the WRITE statement or the fourth loop pass.
         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.
         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.
Object:
Write a  small report using DO loop with EXIT,CHECK and CONTINUE
statement. use SY-INDEX for writing output.
The output should be as follows.
1         3     4   for CONTINUE  statement
2        3            for CHECK statement
1        2           for EXIT      statement    
 

SAP-ABAP Copyright © 2011 - |- Template created by Vishnu - |- Powered by Blogger Templates