You are on page 1of 9

Day-02-Program Flow Control

Friday, July 07, 2017 9:34 AM

Control Structures
A program is designed to solve a task which, in turn, consists of subtasks.
Some of the subtasks must always be performed, others need to
be performed only under certain conditions, and some subtasks are
optional.

Screen clipping taken: 7/7/2017 9:45 AM

Structure start and end

A control structure has a structure start and a structure end. In example, the
structure start is represented by the IF statement, while the ENDIF statement
represents the structure end.

Using Patterns
Like all other ABAP statements, you must also include the statements for the
control structures in the source code. Under certain circumstances, it may
make sense to use patterns for that because patterns insert predefined
structures into the source code from the cursor position onwards.
To insert a pattern, you must position the cursor in the line from which you
want to insert the pattern going forward, and click the Pattern button .

GBERP-ABAP Classes Page 1


GBERP-ABAP Classes Page 2
Screen clipping taken: 7/7/2017 10:23 AM GB TRAINING & PLACEMENT CENTRE 9988111278

The system then opens a window from which you select a pattern from
a range of frequently used patterns, such as the SELECT statement. Other
patterns such as the IF structure can be obtained via the Other pattern
field. Because you want to insert an IF structure, you must enter
IF in that field and click the Continue button (the green arrow icon). The system
then inserts the complete IF structure description into the source code .

Screen clipping taken: 7/7/2017 10:27 AM

GBERP-ABAP Classes Page 3


Screen clipping taken: 7/7/2017 10:27 AM

Lines that aren’t needed should be deleted or converted into comment


lines, and missing parts (such as conditions and statement blocks)
should be added.
IF Structure
Let’s suppose that you want to count only the members of the “ABAP”
course. You could do that in the following way: First process all records
of the table in the usual way. But whenever a record meets this specific
condition, increase the number of members by 1.

IF STUDENT-COURSE = 'ABAP'.
COUNTER_ABAP = COUNTER_ABAP + 1.
ENDIF.
ELSE
In the first statement block of our example, we count the members of
the 'ABAP' course. In the second statement block, we create a counter
for all other members. This means that a count is made in any case.
Which counter will be increased simply depends on the condition.

IF STUDENT-COURSE = 'ABAP'.
GBERP-ABAP Classes Page 4
IF STUDENT-COURSE = 'ABAP'.
COUNTER_ABAP = COUNTER_ABAP + 1.
ELSE.
COUNTER_OTHER = COUNTER_OTHER + 1.
ENDIF.

ELSEIF
The third method of using an IF statement once again checks a condition.
If that condition is not met, a second condition is checked using the ELSEIF
statement. If that second condition is not fulfilled, a third condition is checked
using the ELSEIF statement, and so on.
This process can continue endlessly until you use an ELSE statement to stop it.

IF STUDENT-COURSE = 'ABAP'.
COUNTER_ABAP = COUNTER_ABAP + 1.
ELSEIF STUDENT-COURSE = 'SAPMM'.
COUNTER_MM = COUNTER_MM + 1.
ELSEIF STUDENT-COURSE = 'SAPSD'.
COUNTER_SD = COUNTER_SD + 1.
ELSE.
COUNTER_OTHER = COUNTER_OTHER + 1.
ENDIF.

CASE Structure
To describe the CASE structure, Let's take another look at a real-life
situation. Pedestrian traffic lights can show either red or green light.
If the light is red, we stop; if the light is green, we walk.
Like the single-minded pedestrian, the CASE structure only “looks at”
an object. For the pedestrian, the object is the pedestrian light; for the

If the condition corresponds to the field content, then—and only then—the


associated statement block is executed. A CASE structure only ever processes
one statement block. Once the statement block has been processed, the system
branches to ENDCASE without checking any other field contents. After that,
it proceeds to the first command after the CASE structure, where it continues
the program flow.
GBERP-ABAP Classes Page 5
the program flow.

CASE STUDENT-COURSE .
WHEN 'ABAP'.
COUNTER_ABAP = COUNTER_ABAP + 1.
WHEN 'SAPMM'.
COUNTER_MM = COUNTER_MM + 1.
WHEN OTHERS.
COUNTER_OTHER = COUNTER_OTHER + 1.
ENDCASE.

Loops

DO Loop
The structure of the DO loop begins with a DO statement and ends with
ENDDO. In-between these two statements there is the statement block
to be repeated.
DO.
* statement_block
ENDDO.

In order to limit the number of loop runs, you can define an upper limit of
runs. For example, if you want a loop to be run 12 times, you must use the
following statement: DO 12 TIMES.

Exp- Print First 'n' Numbers.

GBERP-ABAP Classes Page 6


Screen clipping taken: 7/7/2017 11:23 AM

Condition within the DO loop

The most important aspect about the DO loop is that when it executes the
statement block in the structure for the first time, the system doesn’t carry out
any prior check of a condition. Even if you include a condition for terminating the
loop, the commands that are located before the termination condition will be
executed at least once. For this reason, the location of the termination condition
within the statement block is essential for the program logic.

WHILE Loop

In contrast to the DO loop, the WHILE loop checks a condition before it executes
the statement block for the first time. If the condition is met, the statement block
will be executed. Then the system checks again whether the condition is still met.
GBERP-ABAP Classes Page 7
will be executed. Then the system checks again whether the condition is still met.
If so, the statement block is executed once again, and so on. This process
continues endlessly unless the condition is no longer met, for instance, because it
was modified within the statement block or a termination condition has been
reached

Screen clipping taken: 7/7/2017 11:31 AM

Logical Expressions

Simple Logical Expressions


These logical operators allow you to formulate conditions in order to compare
two fields.
Example:

IF NUMBER01 > NUMBER02.


* statement_block
ENDIF.

GBERP-ABAP Classes Page 8


Screen clipping taken: 7/7/2017 11:35 AM

Special Logical Operators

Special logical operators are available for character strings. You can use
them to formulate conditions in order to find out whether a string contains
certain characters and so on. In this context, only the check for an initial value
of a field and the check for an interval are worth mentioning.

IF number01 IS INITIAL.
* statement_block
ENDIF.

GBERP-ABAP Classes Page 9

You might also like