Tuesday, 7 January 2014

Program Flow Control and Logical Expressions in SAP ABAP

Program Flow Control and Logical Expressions in SAP ABAP

Control Structures
This section will look at program flow control and logical expressions. It could be argued that this is really the main aspect of ABAP programming, where the real work is done. How one structures a program using logical expressions will determine the complete flow of the program and in what sequence actions are taken.
First, a look will be taken at control structures. When a program is created it is broken up into many tasks and subtasks. One controls how and when the sections of a program are executed using logical expressions and conditional loops, often referred to as control structures
If Statement
Copy you program previous chapter in which to test some of the logic which is to be built. Here I copy the program Z_OPENSQL_1 to Z_LOGIC_1.
               
Program Flow Control and Logical Expressions in SAP ABAP

Remove all of the code from the program, leaving only the first example INSERT statement and its validation test.
When one talks of control structures, this refers to large amounts of code which allows one to make decisions, resulting in a number of different outcomes based on the decisions taken. Take a look at the IF statement to explain the basic logic at work here.
The IF statement is probably the most common control structure, found in just about every programming language. The syntax may vary between languages, but its use is just about universal:

Program Flow Control and Logical Expressions in SAP ABAP

            
This IF statement tells the program that IF (a logical expression), do something. The ELSE addition means that should this logical expression not occur, do something else. Then the statement is ended with the ENDIF statement.
The IF and ENDIF statements belong together, and every control structure created will take a similar form, with a start and an end. Control structures can be very large, and may contain other, smaller control structures within them, having the system perform tasks within the framework of a larger task. The code between the start and end of a control structure defines the subtasks within it. Tasks can be repeated, in what are called loops.
From here on, control structures will be used to control the flow, create tasks, subtasks and branches within a program, and to perform loops.
Comment out all of the preceding code, and click the ‘Pattern’ button, in the toolbar by Pretty Printer. A window will appear, and just select the ‘Other pattern’ field, and type “IF”. The structure of an IF statement will then appear in the code, which can be followed as a guide
            
Program Flow Control and Logical Expressions in SAP ABAP

Program Flow Control and Logical Expressions in SAP ABAP

           
Create a DATA statement, 15 characters of type ‘c’, and name this “surname”. Then on a new line give this the value ‘SMITH’. Then edit the auto-generated IF statement so that it looks like this
           
Program Flow Control and Logical Expressions in SAP ABAP

The IF statement here takes the form that if the value of ”surname” is ‘SMITH’, text will be displayed stating “Youve won a car!” (note that an apostrophe cannot be placed correctly in You’ve without making the code invalid). Then execute the code. The result should be:
                          
Program Flow Control and Logical Expressions in SAP ABAP

Next, this will be extended to include the ELSEIF statement which has been commented out above. Change the value of “surname” to ‘BROWN’. Then, add to the ELSEIF statement so that if the value of “surname” is ‘BROWN’, the output text will read “Youve won a boat!”
        
Program Flow Control and Logical Expressions in SAP ABAP


                       
Program Flow Control and Logical Expressions in SAP ABAP

In this example, the first IF statement was not true, as the surname was not Smith. Hence this branch was not executed. The ELSEIF statement was true, so the text output assigned
here appeared. The ELSEIF statement can be added to an IF statement any number of times, to designate the action taken in a number of situations
Program Flow Control and Logical Expressions in SAP ABAP
                        
Depending on what the value of ‘surname’ is at any given time, a different branch will be executed.
There is also the ELSE statement. This is used for the last piece of the IF block, and is used if none of the values in the IF and ELSEIF statement are matched. The full block of code is shown below
Program Flow Control and Logical Expressions in SAP ABAP
                      

With this block as it is now, there will always be an output, regardless of the value of ‘sur-name’, every possibility is now taken care of. The value will either match one of the first four, or the ELSE statement’s text will be displayed. The IF statement is very important for determining the flow of a program and will be used on a regular basis

No comments:

Post a Comment